#42813 closed feature request (invalid)
the_posts_pagination() should have second parameter
Reported by: | khoipro | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Query | Keywords: | |
Focuses: | template | Cc: |
Description
Hi,
When working with the_posts_pagination(), I found it does not work well with Custom Page Template which contains post list. It's because a function get_the_posts_pagination uses condition $GLOBALSwp_query?->max_num_pages > 1 before outputing.
As you know, the custom page template cannot make $wp_query to display a list of posts in this case.
A better solution is keeping the second parameter which keeps $GLOBALSwp_query? as a default argument.
A complete optimized function should be:
<?php function get_the_posts_pagination( $args = array(), $the_query = $GLOBALS['wp_query'] ) { $navigation = ''; // Don't print empty markup if there's only one page. if ( $the_query->max_num_pages > 1 ) { $args = wp_parse_args( $args, array( 'mid_size' => 1, 'prev_text' => _x( 'Previous', 'previous set of posts' ), 'next_text' => _x( 'Next', 'next set of posts' ), 'screen_reader_text' => __( 'Posts navigation' ), ) ); // Make sure we get a string back. Plain is the next best thing. if ( isset( $args['type'] ) && 'array' == $args['type'] ) { $args['type'] = 'plain'; } // Set up paginated links. $links = paginate_links( $args ); if ( $links ) { $navigation = _navigation_markup( $links, 'pagination', $args['screen_reader_text'] ); } } return $navigation; }
Related Cases
Someone tried to remove default $wp_query and replace with a custom query which I don't feel safe at all:
https://codeplanet.io/wordpress-paginate-wp_query/
If we don't use the_posts_pagination(), we must make a custom pagination which is very waste time to re-do something WordPress the_posts_pagination should do:
http://web-profile.net/wordpress/themes/wordpress-custom-loop/
Thank you,