skip to Main Content

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(); 

?>

2

Answers


  1.               <?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 } ?>
    
    Login or Signup to reply.
  2.    <?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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search