php - How can I wrap page numbers from the_posts_pagination in WordPress into my own div? -
in wordpress, i'm using the_posts_pagination
spit out prev/next buttons , page numbers in between. don't how wordpress spits out own markup when i'd group elements divs.
this current code:
php
<?php the_posts_pagination( array( 'mid_size' => 2, 'prev_text' => __( 'previous page', 'textdomain' ), 'next_text' => __( 'next page', 'textdomain' ), 'screen_reader_text' => ( '' ) ) ); ?>
it spits out this:
html
<nav class="navigation pagination" role="navigation"> <div class="nav-links"> <a class="prev page-numbers" href="#">prev page</a> <a class="page-numbers" href="#">1</a> <span class="page-numbers current">2</span> <a class="page-numbers" href="">3</a> <a class="page-numbers" href="">4</a> <a class="next page-numbers" href="">next page</a> </div> </nav>
i'd ability make this:
<nav class="navigation pagination" role="navigation"> <div class="nav-links"> <a class="prev page-numbers" href="#">prev page</a> <div class="page-numbers-container"> <a class="page-numbers" href="#">1</a> <span class="page-numbers current">2</span> <a class="page-numbers" href="">3</a> <a class="page-numbers" href="">4</a> </div> <a class="next page-numbers" href="">next page</a> </div> </nav>
even if wrap next , previous links in div that'd great too. don't want have edit core wordpress files. need write function?
edit
this in index.php
<?php the_posts_pagination( wp_custom_pagination(['prev_text' => __( 'previous page', 'textdomain' ), 'next_text' => __( 'next page', 'textdomain' )])); ?>
this put inside functions.php
function wp_custom_pagination($args = [], $class = 'pagination') { if ($globals['wp_query']->max_num_pages <= 1) return; $args = wp_parse_args( $args, [ 'mid_size' => 2, 'prev_next' => false, 'prev_text' => __('older posts', 'textdomain'), 'next_text' => __('newer posts', 'textdomain'), 'screen_reader_text' => __('posts navigation', 'textdomain'), ]); $links = paginate_links($args); $next_link = get_previous_posts_link($args['next_text']); $prev_link = get_next_posts_link($args['prev_text']); $template = apply_filters( 'navigation_markup_template', ' <nav class="navigation %1$s" role="navigation"> <h2 class="screen-reader-text">%2$s</h2> <div class="nav-links">%3$s<div class="page-numbers-container">%4$s</div>%5$s</div> </nav>', $args, $class); echo sprintf($template, $class, $args['screen_reader_text'], $prev_link, $links, $next_link); }
the_posts_pagination() uses paginate_links() has no filter alter output want.
so, let's create custom pagination template:
function the_so37580965_wp_custom_pagination($args = [], $class = 'pagination') { if ($globals['wp_query']->max_num_pages <= 1) return; $args = wp_parse_args( $args, [ 'mid_size' => 2, 'prev_next' => false, 'prev_text' => __('older posts', 'textdomain'), 'next_text' => __('newer posts', 'textdomain'), 'screen_reader_text' => __('posts navigation', 'textdomain'), ]); $links = paginate_links($args); $next_link = get_previous_posts_link($args['next_text']); $prev_link = get_next_posts_link($args['prev_text']); $template = apply_filters( 'the_so37580965_navigation_markup_template', ' <nav class="navigation %1$s" role="navigation"> <h2 class="screen-reader-text">%2$s</h2> <div class="nav-links">%3$s<div class="page-numbers-container">%4$s</div>%5$s</div> </nav>', $args, $class); echo sprintf($template, $class, $args['screen_reader_text'], $prev_link, $links, $next_link); }
try out. also, take @ paginate_links() function, there're many more arguments may need.