Skip to content
Advertisement

Page displaying a specific category has pagination problem

I have a client that has a Static WordPress website with a blog.

The main blog page (home.php) works great with pagination all correct.

Note: I am using the WP-PageNavi to do the pagination for me.

I have also created a customized template page that displays the posts from a specific category (recipes). The pagination code doesn’t work correctly on this page. There should be two pages available, and it only shows that there is one.

I know the issue is that I need to tweak the coding for the pagination on the recipe page, but I don’t really know what to do.

This is the recipe (category) blog page link: www.aphrodisiacsexpert.com/aphrodisiacs-expert-blog/aphrodisiac-recipes/

Here is the code that displays the page with the recipes blog posts along with its pagination:

            <?php
            $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
            $args = array(
                'post_type' => 'post',
                'post_status' => 'publish',
                'order'=> 'DESC', 
                'orderby' => 'post_date', 
                'category_name' => 'Recipes',
                'posts_per_page' => 9,
                'paged' => $paged,
            );
            $postslist = get_posts( $args );
            foreach ($postslist as $post) :  setup_postdata($post); ?> 
                <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 home-blog-list" style="float: left; display:block">                
                    <center>
                      <div class="img-responsive box-shadow shadow-effect" style="">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                      </div>
                      <h3 class="entry-title script" style="text-align: center;">
                        <a class="entry-title-link" href="<?php the_permalink(); ?>"><?php the_title(); ?>
                        </a>
                      </h3>
                    <center>
                </div>
            <?php endforeach; ?>
            <?php wp_pagenavi(); ?>

Can anyone give me some ideas on how to fix this problem?

Thanks, SunnyOz

Advertisement

Answer

After many hours of researching, I finally found one question similar to mine (from 10 years ago) with a great solution. It can be found here: https://wordpress.stackexchange.com/questions/4696/pagination-not-working-with-custom-loop.

The answer that helped me was provided by @ChowKaiDeng (combo of comment from @Jan Fabry and original code by @nurain)

I thought I would post the answer here to show that the solution still works in 2020 with a current version of WordPress and the WP-PageNavi plugin. (The problem appears to be with the PageNavi and the $wp_query variable.)

Here is my code updated with the working solution:

        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $myquery = new WP_Query(
            array(
                'posts_per_page' => '9',
                'paged'=>$paged,
                'post_type' => 'post',
                'post_status' => 'publish',
                'order'=> 'DESC', 
                'orderby' => 'post_date', 
                'category_name' => 'Recipes',
            )   
        );  
        ?>

        <?php
        if ($myquery->have_posts()) :  while ($myquery->have_posts()) : $myquery->the_post();
        ?>

        <!-- Start your post -->

            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 home-blog-list" style="float: left; display:block">                
                <center>
                  <div class="img-responsive box-shadow shadow-effect" style="">
                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                  </div>
                  <h3 class="entry-title script" style="text-align: center;">
                    <a class="entry-title-link" href="<?php the_permalink(); ?>"><?php the_title(); ?>
                    </a>
                  </h3>
                <center>
            </div>

        <!-- End of your post -->

        <?php endwhile; ?>
        <?php wp_pagenavi( array( 'query' => $myquery ) ); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
        <?php wp_reset_query(); ?>
        <?php else : ?>
        <p>No posts found</p>
        <?php endif; ?>         
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement