Home > OS >  Why isnt my pagination working in the while post loop?! Help would be much appreciated, new to PHP
Why isnt my pagination working in the while post loop?! Help would be much appreciated, new to PHP

Time:04-07

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 >
        <div >
    <?php
        while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
            <div >
            <!-- // all content, title, meta fields-->
                <section><a  href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
                <h4 ><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  role="navigation">
                <?php if($previousLink || $paged > 1 ) { ?>
                    <div >
                    <button ><?php previous_posts_link( 'Previous Page',  ); ?></button>
                    </div>
                <?php } 
                if($nextLink) { ?>
                    <div >
                        <button >
                        <?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(); 

?>

CodePudding user response:

              <?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 >
                    <div >
                <?php
                    while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
                        <div >
                        <!-- // all content, title, meta fields-->
                            <section><a  href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
                            <h4 ><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  role="navigation">
                            <?php if($previousLink || $paged > 1 ) { ?>
                                <div >
                                <button ><?php previous_posts_link( 'Previous Page',  ); ?></button>
                                </div>
                            <?php } 
                            if($nextLink) { ?>
                                <div >
                                    <button >
                                    <?php next_posts_link( 'Next Page ', $all_catPosts->max_num_pages );?>
                                    </button>
                                </div>
                            <?php } ?>
                        </nav>
                    <?php endif;?>
                    <!-- end pagination -->
            
                </div><!--/ end of container-->
            
                <?php } ?>

CodePudding user response:

   <?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;
  • Related