简体   繁体   中英

wordpress - WP_Query, pagination not working

I am trying to get working pagination in my WP_Query . After trying more then 2 hour, no way except stackoverflow :-D.

What is my problem


Older and newer pagination links are appreaing and when I click on them, then it take me to the correct url which is : /?paged=2 .

But post list did not change, same post on every page.

Here is my code


$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$args = array(
    'cat' => $cat,
    (($paged != '') ? 'paged =>'. $paged : ''),
    'posts_per_page' => $post_to_show 
);  

print_r($args);

$the_query = new WP_Query($args); 

while ($the_query->have_posts()) :  $the_query->the_post();         

     //post template            

endwhile;

if ( get_next_posts_link() || get_previous_posts_link() ) {
    echo '<div class="wp-navigation clearfix">
    <div class="alignleft">'.next_posts_link('&laquo; Older Entries').'</div>
    <div class="alignright">'.previous_posts_link('Newer Entries &raquo;').'</div>
    </div>';
}


wp_reset_query();

Your $args array looks wrong. Also, $paged will never be empty (because it always gets assigned the default value of 1), so your check is redundant.

There is nothing wrong with passing 1 as page number.

$args = array(
    'cat' => $cat,
    'paged' => $paged,
    'posts_per_page' => $post_to_show 
);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM