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



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

博客主页(home.php(的分页非常正确。

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

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

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

这是食谱(类别(博客页面链接:www.aphrodisicsexpert.com/aphrodisiacs-expert-blog/aphrodisia-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; ?>         

最新更新