php - 显示特定类别的页面存在分页问题
问题描述
我有一个客户端,它有一个带有博客的静态 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
解决方案
经过数小时的研究,我终于找到了一个与我(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; ?>
推荐阅读
- python - Django 迁移值无效文字
- javascript - vue-bootstrap-datetimepicker 中缺少指向下个月和上个月的箭头?
- docker - 在另一个容器的构建过程中无法解析 docker 容器
- ios - 从 UIDocumentBrowserViewController 获取 UIActivityViewController 的 sourceRect
- javascript - Javascript - 比较两个数组对象并附加值
- python - TensorFlow中的特定线性分类器:输入元素作为向量
- reactjs - 一个屏幕上有多个带有过滤器的 React 管理员列表
- javascript - 检查提及是否是用户 - 不和谐
- python - 使用python将sticky位分配给linux组权限
- docker - 安装 Catalina 后 Docker Sync 无法启动