显示特定类别的页面存在分页问题

Page displaying a specific category has pagination problem

我有一个客户有一个带博客的静态 WordPress 网站。

博客主页面 (home.php) 的分页效果很好。

注意: 我正在使用 WP-PageNavi 为我做分页。

我还创建了一个自定义模板页面,显示来自特定类别(食谱)的帖子。分页代码在此页面上无法正常工作。应该有两页,现在只显示有一页

我知道问题是我需要调整食谱页面上的分页编码,但我真的不知道该怎么做。

这是菜谱(类别)博客页面link:www.aphrodisiacsexpert.com/aphrodisiacs-expert-blog/aphrodisiac-recipes/

以下是显示包含食谱博客帖子的页面及其分页的代码:

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

任何人都可以给我一些解决此问题的想法吗?

谢谢, 阳光明媚

经过几个小时的研究,我终于找到了一个与我的问题(10 年前)相似的问题,并且有很好的解决方案。可以在这里找到:https://wordpress.stackexchange.com/questions/4696/pagination-not-working-with-custom-loop.

@ChowKaiDeng 提供了对我有帮助的答案(@Jan Fabry 的评论和@nurain 的原始代码的组合)

我想我会 post 这里的答案表明该解决方案在 2020 年仍然适用于当前版本的 Wordpress 和 WP-PageNavi 插件。 (问题似乎出在 PageNavi 和 $wp_query 变量上。)

这是我的代码更新后的工作解决方案:

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