首页 > 解决方案 > 显示特定类别的页面存在分页问题

问题描述

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

主博客页面(home.php)与分页都正确无误。

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

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

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

这是食谱(类别)博客页面链接: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(); ?>

谁能给我一些关于如何解决这个问题的想法?

谢谢, SunnyOz

标签: phpwordpresspaginationcategories

解决方案


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

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

我想我会在这里发布答案,以表明该解决方案在 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; ?>         

推荐阅读