置顶帖 are a great way to highlight your featured content. However, there are certain places on your website where you don’t need sticky posts to be on the top. WordPress can not guess this so you need to explicitly tell WordPress to exclude sticky posts from a custom loop. In this article, we will show you how to completely exclude sticky posts from the loop in WordPress, and we will also show you how you can take away the sticky feature of the post, so it still shows in their natural order.
便利贴是突出显示特色内容的好方法。 但是,您网站上的某些位置不需要在顶部放置粘性帖子。 WordPress无法猜测这一点,因此您需要明确告诉WordPress从自定义循环中排除粘性帖子。 在本文中,我们将向您展示如何从WordPress的循环中完全排除粘性帖子,并且还将向您展示如何删除帖子的粘性功能,使其仍然按其自然顺序显示。
如何带走岗位的黏性 (How to take away the Sticky Ability of the Post)
When you are displaying most recent posts in a tab, you do not want the sticky posts to stay sticky. If you do not remove the sticky feature, the recent posts area would be useless as all your sticky posts will crowd this area. This is where query_posts
feature comes in handy.
当您在选项卡中显示最新帖子时,您不希望粘性帖子保持粘性。 如果您不删除粘性功能,则最近的帖子区域将无用,因为您所有的粘性帖子都将拥挤该区域。 这是query_posts
功能派上用场的地方。
为此,您需要将循环更改为如下所示:
为此,您需要将循环更改为以下内容:
<?php
$args = array(
'posts_per_page' => 10,
'ignore_sticky_posts' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
此代码忽略帖子是粘性的并以正常顺序显示帖子。使用此代码,您的置顶帖子将出现在循环中,但它们不会放置在顶部。
此代码忽略置顶的帖子并按正常顺序显示帖子。使用此代码,您的置顶帖子将出现在循环中,但不会放置在顶部。

从循环中完全排除粘性帖子 (Completely exclude Sticky posts from the Loop)
如果您在滑块中使用粘性帖子,那么有时您可能希望将粘性帖子完全排除在循环之外。您所要做的就是编辑自定义循环以与之匹配:
如果您在滑块中使用卡片,有时您可能希望将自己的卡片从循环中排除。您需要做的就是编辑自定义循环以匹配此:
<?php
$the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>
This code will not display any sticky posts in the post loop. For more tips on modifying WordPress themes, check out our WordPress 主题备忘单 for beginners.
此代码不会在发布循环中显示任何粘性发布。 有关修改WordPress主题的更多提示,请查阅针对初学者的WordPress 主题备忘单 。
Source: WP 法典
资料来源: WP 法典
翻译自: https://www.wpbeginner.com/wp-themes/how-to-exclude-sticky-posts-from-the-loop-in-wordpress/