[wp-trac] [WordPress Trac] #42813: the_posts_pagination() should have second parameter
WordPress Trac
noreply at wordpress.org
Wed Dec 6 09:09:33 UTC 2017
#42813: the_posts_pagination() should have second parameter
-----------------------------+-----------------------------
Reporter: khoipro | Owner:
Type: feature request | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version: 4.9.1
Severity: normal | Keywords:
Focuses: template |
-----------------------------+-----------------------------
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
$GLOBALS['wp_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
$GLOBALS['wp_query'] as a default argument.
A complete optimized function should be:
{{{#!php
<?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,
--
Ticket URL: <https://core.trac.wordpress.org/ticket/42813>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list