WordPress have the_posts_navigation
function since 4.1.0. But I don't know how to use with wp_query
or get_posts
. the following code is in a template file of page.
wp_query method:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$get_posts=new wp_query('post_type=case&posts_per_page=2&paged='.$paged);
while($get_posts->have_posts()):$get_posts->the_post();
the_title();
endwhile;
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'cm' ),
'next_text' => __( 'Next page', 'cm' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
) );
?>
get_posts method:
<?
while(have_posts()):the_post();
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$case_posts=get_posts('post_type=case&posts_per_page=2&paged='.$paged);
echo '<pre>';
print_r($case_posts);
echo '</pre>';
foreach($case_posts as $case_post){
echo $case_post->post_title;
}
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'cm' ),
'next_text' => __( 'Next page', 'cm' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
) );
?>
They don't work and display pagination, but entering http://127.0.0.1/gdboer/?page_id=74&page=2 manually in address bar, it works. Who can help me, thank you so much!
WordPress have the_posts_navigation
function since 4.1.0. But I don't know how to use with wp_query
or get_posts
. the following code is in a template file of page.
wp_query method:
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$get_posts=new wp_query('post_type=case&posts_per_page=2&paged='.$paged);
while($get_posts->have_posts()):$get_posts->the_post();
the_title();
endwhile;
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'cm' ),
'next_text' => __( 'Next page', 'cm' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
) );
?>
get_posts method:
<?
while(have_posts()):the_post();
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$case_posts=get_posts('post_type=case&posts_per_page=2&paged='.$paged);
echo '<pre>';
print_r($case_posts);
echo '</pre>';
foreach($case_posts as $case_post){
echo $case_post->post_title;
}
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'cm' ),
'next_text' => __( 'Next page', 'cm' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'cm' ) . ' </span>',
) );
?>
They don't work and display pagination, but entering http://127.0.0.1/gdboer/?page_id=74&page=2 manually in address bar, it works. Who can help me, thank you so much!
Share Improve this question edited Apr 3, 2016 at 7:28 Jevuska 1,17010 silver badges21 bronze badges asked Jan 15, 2015 at 2:09 Vincent WongVincent Wong 1711 gold badge2 silver badges7 bronze badges 1 |3 Answers
Reset to default 10the_posts_navigation()
is simply a wrapper function for get_the_posts_navigation()
which issimply a wrapper function for paginate_links
. The first two functions uses the the same exact parameters that is being used by paginate_links
and actually passes it to the latter function as well
get_the_posts_navigation()
and the_posts_navigation()
is good new functions as it eliminates a lot of custom coding and it is more user friendly for new unexperienced users who would like numbered pagination links
The only flaw in this get_the_posts_navigation()
is that the developers went and wrapped the paginate_links
function in a conditional statement that states that if the main query ($wp_query
) has less than 1 page, (remember, the first page is 0
and the second page is 2
), don't show the links. This is problematic for custom queries on page templates. Pages will always have just one page, so these functions does not work with custom queries
The only true workaround if you have to use the_posts_navigation()
, is to make use of @ChipBennet answer in this post. I really don't like nullifying the main query (quite hacky, in my opinion this is just like using query_posts
) but I can't see any other solution to make get_the_posts_navigation()
to work with custom queries
I have a custom template and I struggled hours to show the pagination component. here what's worked for me.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 3,
'orderby' => 'menu_order',
'order'=> 'ASC',
'paged'=>$paged,
'post_type' => 'projects'
);
$projects = new WP_Query($args);
<!-- working example of pagination with numbers -->
...<?php endwhile;?>
<?php
$GLOBALS['wp_query']->max_num_pages = $projects->max_num_pages;
the_posts_pagination( array(
'mid_size' => 1,
'prev_text' => __( 'Back', 'green' ),
'next_text' => __( 'Onward', 'green' ),
'screen_reader_text' => __( 'Posts navigation' )
) ); ?>
OR
<!-- working example of pagination without numbers -->
...<?php endwhile;?>
<?php next_posts_link( 'next', $projects->max_num_pages ); ?>
<?php previous_posts_link('prev') ?>
This function uses the get_the_posts_pagination()
which uses the GLOBAL wp_query
to setup the paginate_links()
function, so I believe that doesn't work for get_posts
.
Try use the function paginate_links()
by itself or the function posts_nav_link()
PS: Make sure you use wp_reset_query()
the_posts_pagination
, it's different with your title questionthe_posts_navigation
. – Jevuska Commented Apr 2, 2016 at 20:02