[英]WordPress posts not paginating correctly
我的WordPress帖子有一个奇怪的问题。 我有一个静态首页和一个称为Blog的帖子页。 我有一个带有标准发布循环的home.php模板。
我已将管理员中的帖子设置为最多仅显示8个帖子,并且我有<?php previous_posts_link(); ?><?php next_posts_link(); ?>
<?php previous_posts_link(); ?><?php next_posts_link(); ?>
<?php previous_posts_link(); ?><?php next_posts_link(); ?>
链接。 但是,当我启用最大帖子数限制和分页时,我的帖子顺序会出错。
我没有依次显示帖子1 2 3 4 5 6 7 8然后9 10,而是在第一页上看到3 1 10 9 8 7 6 5在第二页上看到了9 10 4 1。 帖子9和10似乎重复了一次,而帖子2根本没有显示。
我尝试过wp_reset_postdata()
并使用wp_query
设置顺序和编号,但是我仍然遇到相同的问题。 我当前的模板是:
<?php if ( have_posts() ) : ?>
<main id="main" class="site-main" role="main">
<!-- start content -->
<?php if ( is_home() && ! is_front_page() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php single_post_title(); ?></h1>
</header>
<?php endif; ?>
<?php
$i = 1;
$count_posts = wp_count_posts();
if ( $count_posts ) {
$published_posts = count( $count_posts->publish );
}
while ( have_posts() ) :
the_post();
if ( $i > 1 && $i !== $published_posts ) {
echo '<hr />';
}
get_template_part( 'partials/content', get_post_format() );
$i++;
endwhile;
?>
<!-- end content -->
<!-- start post nav -->
<?php previous_posts_link(); ?>
<?php next_posts_link(); ?>
<!-- end post nav -->
</main>
<!-- if no content -->
<?php
else :
get_template_part( 'partials/content', 'none' );
endif;
?>
当尝试wp_query
我做到了:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 8,
'orderby' => 'menu_order',
'order' => 'DESC',
'post_status' => 'publish',
'paged' => '$paged',
);
$loop = new WP_Query( $args );
我在调用分页链接后以wp_reset_postdata()
。
我只是认为是什么可能导致了此问题,所以我想知道你们中的任何一个是否可以提供帮助?
感谢您抽出宝贵的时间阅读我的帖子。
因此,我发现了从未有过的问题。 在Pankaj Verma和charan kumar的上述评论的帮助下,我检查了args中的menu_order
参数。 嘿,presto,在将menu_order
更改为另一个orderby参数时,它解决了我的问题。
在Google之后,我发现许多人的menu_order都存在相同的问题。 以我个人的经验,使用它之前从未发生过。 但这似乎是一个普遍的问题。
因此,为解决我的问题,我重新添加了wp_query并将选项更改为:
$args = array(
'post_type' => 'post',
'posts_per_page' => 8,
'orderby' => 'ID',
'order' => 'DESC',
'post_status' => 'publish',
'paged' => $paged,
);
希望这对遇到类似问题的其他人有所帮助,并感谢所有对我的帖子发表评论的人,他们对解决这一问题确实有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.