[英]Wordpress showing same posts on each page
I think it has something to do with the main loop.我认为这与主循环有关。 I don't want to include a category in the main loop so I have done the following.我不想在主循环中包含一个类别,所以我做了以下工作。
However, it just shows the most recent posts on each page when I go to the next one in the pagination.但是,当我转到分页中的下一个时,它只显示每个页面上的最新帖子。 What am I doing wrong?我究竟做错了什么?
<?php query_posts('cat=-219'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<div style="clear:both"></div>
<nav>
<div class="pager">
<div class="pager-btn" id="next-pager"><?php next_posts_link( 'Next' ); ?></div>
<div class="pager-btn" id="last-pager"><?php previous_posts_link( 'Last' ); ?></div>
</div>
</nav>
<?php endif; wp_reset_query(); ?>
I've tried changing it to only include the categories I want instead of excluding the one but it still does the same.我尝试将其更改为仅包含我想要的类别而不是排除一个类别,但它仍然是相同的。 It works fine if I just include one category though (eg I put cat=1
).如果我只包含一个类别(例如我放cat=1
),它就可以正常工作。
I can recommend you to make queries with proper arguments, like this:我可以建议您使用适当的参数进行查询,如下所示:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 3 (or -1 for all post),
'cat'=>219
'post_status'=>'publish',
'order' => 'DESC',
'paged' => $paged
);
$the_query = new WP_Query($args);
Then in your loop, you could do this:然后在你的循环中,你可以这样做:
if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<div style="clear:both"></div>
<nav>
<div class="pager">
<?php if (function_exists("pagination")){pagination($custom_query->max_num_pages);} ?>
</div>
</nav>
<?php endif; wp_reset_query();?>
Then copy the Pagination Code in functions.php:然后复制functions.php中的Pagination Code:
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
More information you can find at https://developer.wordpress.org/reference/functions/query_posts/您可以在https://developer.wordpress.org/reference/functions/query_posts/找到更多信息
Solved by adding this to functions.php.通过将其添加到functions.php 来解决。
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-219' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.