0

I have this code down below, showing 16 posts and on the next page the next 16 but right now it's not getting new posts on the second + page. It is showing the same first 16 that it shows on the first page. I don't know where I went wrong and why its not communicating with the while loop?

    $cat = get_the_category();
    $cat_name = esc_html($cat[0]->name);
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => $cat_name,
        'posts_per_page' => 16,
        'paged' => $paged,
        'orderby' => 'date',
        'order' => 'DESC',
        'offset' => 5

    );

    $all_catPosts = null;
    $all_catPosts = new WP_Query($args);

    if ($all_catPosts->have_posts()) {

        ?>
    <!-- 16 category container with the next page pagination button -->
    <div class="min-h-screen flex items-center justify-center">
        <div class="grid grid-cols-4">
    <?php
        while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
            <div class="p-5 rounded big_cat_container">
            <!-- // all content, title, meta fields-->
                <section><a class="thumbnail_img" href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
                <h4 class="post_title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h4>
            </div>
        <?php endwhile;?>
        </div><!--/ end of grid-->

        <!-- //pagination buttons -->
        <?php 
        $previousLink = get_previous_posts_link();
        $nextLink = get_next_posts_link();
        $hasNextPage = $previousLink || $nextLink;

        if ($hasNextPage):?>
            <nav class="pagination" role="navigation">
                <?php if($previousLink || $paged > 1 ) { ?>
                    <div class="page-btn nav-next">
                    <button class="previous-btn"><?php previous_posts_link( 'Previous Page',  ); ?></button>
                    </div>
                <?php } 
                if($nextLink) { ?>
                    <div class="page-btn nav-previous">
                        <button class="next-btn">
                        <?php next_posts_link( 'Next Page ', $the_query->max_num_pages );?>
                        </button>
                    </div>
                <?php } ?>
            </nav>
        <?php endif;?>
        <!-- end pagination -->

    </div><!--/ end of container-->

    <?php } ?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 

?>
Vanessa O
  • 1
  • 1

2 Answers2

0
              <?php 
                $cat = get_the_category();
                $cat_name = esc_html($cat[0]->name);
                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
            
                $args = array(
                    'post_type' => 'post',
                    'post_status' => 'publish',
                    'category_name' => $cat_name,
                    'posts_per_page' => 16,
                    'paged' => $paged,
                    'orderby' => 'date',
                    'order' => 'DESC',
                    'offset' => 5
            
                );
            
                $all_catPosts = null;
                $all_catPosts = new WP_Query($args);
            
                if ($all_catPosts->have_posts()) {
            
                    ?>
                <!-- 16 category container with the next page pagination button -->
                <div class="min-h-screen flex items-center justify-center">
                    <div class="grid grid-cols-4">
                <?php
                    while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
                        <div class="p-5 rounded big_cat_container">
                        <!-- // all content, title, meta fields-->
                            <section><a class="thumbnail_img" href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
                            <h4 class="post_title"><a href="<?php the_permalink();?>"><?php the_title();?></a></h4>
                        </div>
                    <?php endwhile;?>
                    </div><!--/ end of grid-->
                    <!-- //pagination buttons -->
                    <?php 
                    $previousLink = get_previous_posts_link();
                    $nextLink = get_next_posts_link();
                    $hasNextPage = $previousLink || $nextLink;
            
                    if ($hasNextPage):?>
                        <nav class="pagination" role="navigation">
                            <?php if($previousLink || $paged > 1 ) { ?>
                                <div class="page-btn nav-next">
                                <button class="previous-btn"><?php previous_posts_link( 'Previous Page',  ); ?></button>
                                </div>
                            <?php } 
                            if($nextLink) { ?>
                                <div class="page-btn nav-previous">
                                    <button class="next-btn">
                                    <?php next_posts_link( 'Next Page ', $all_catPosts->max_num_pages );?>
                                    </button>
                                </div>
                            <?php } ?>
                        </nav>
                    <?php endif;?>
                    <!-- end pagination -->
            
                </div><!--/ end of container-->
            
                <?php } ?>
  • Please try now,i just updated the $the_query variable with $all_catPosts object variable. it should work! – Deepak Kumar Dass Apr 06 '22 at 19:02
  • Hi Deepak! Thank you for the help but its still showing the same 16 results, its not looping even after pluggin in the $all_catPosts – Vanessa O Apr 06 '22 at 19:20
  • @VanessaO i have just posted another answer try to use this simple post previous next pagination loop if it works fine then do the parameter modifications as per your requirement. Thank you! – Deepak Kumar Dass Apr 06 '22 at 20:12
0
   <?php
 
   $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
     
    // the query
    $the_query = new WP_Query( array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 16,
        'paged' => $paged
    );
     
    if ( $the_query->have_posts() ) :
        // the loop
        while ( $the_query->have_posts() ) : $the_query->the_post();
            the_title();
             
        endwhile;
     
        // next_posts_link() usage with max_num_pages.
        next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages );
        previous_posts_link( __( 'Newer Entries', 'textdomain' ) );
     
        // Clean up after the query and pagination.
        wp_reset_postdata(); 
     
    else:
        ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ) ); ?></p>
        <?php
    endif;
  • I figured out what was breaking the pagination..its the offet. Apparently using offet with pagination breaks it. I used offet because I want to start at the fifth index of posts. Not at the first hmm – Vanessa O Apr 06 '22 at 22:10
  • Okay, great! You can use offset like this way see solution: https://stackoverflow.com/questions/49227520/how-can-i-paginate-wp-query-results-when-theres-an-offset – Deepak Kumar Dass Apr 06 '22 at 23:06