简体   繁体   中英

wordpress wp_query pagination not working

Here's my code on index.php of the theme. I am using Twenty Twenty-One theme . I have 16 posts in total. I want to show 2 posts per page with pagination but this code returns only first 2 pages in pagination. It doesn't go to page/3, page/4 etc.

In WP dashboard->settings->Reading->Blog pages show at most is set to : 10 posts.
Your homepage displays is set to : Your latest posts

I want to know what's wrong in the code? Why this code is not working? any help will be appreciated.

get_header();

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$the_query = new WP_Query('posts_per_page=2&paged=' . $paged );

if ( $the_query->have_posts() ) {

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

        the_title(); // post Title
    }

    
    twenty_twenty_one_the_posts_navigation();
    
    wp_reset_postdata();

} else {

    echo 'now posts found';

}

get_footer();

If you are using default index.php and want 2 post per page then you can easily do with admin setting 2 instead of 10 which you already know. I also checked and your code is almost fine and seems working fine for me as well as, Please debugging that with other code and other things

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