如何从WordPress中的循环中排除粘滞帖子

粘贴帖子是突出您的精选内容的好方法。但是,您的网站上有一些地方您不需要粘贴帖子。 WordPress无法猜测这一点,因此您需要明确告诉WordPress从自定义循环中排除粘性帖子。在本文中,我们将向您展示如何在WordPress中完全排除循环中的粘性帖子,我们还将向您展示如何删除帖子的粘性功能,因此它仍以自然顺序显示。

如何剥夺帖子的粘性能力

当您在选项卡中显示最新帖子时,您不希望粘性帖子保持粘性。如果您不删除粘性功能,最近的帖子区域将无用,因为所有粘性帖子都会占用此区域。这是哪里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();  ?>  

此代码忽略帖子是粘滞的,并按正常顺序显示帖子。使用此代码,您的粘贴帖子将显示在循环中,但它们不会放在顶部。

Sticky posts displayed in normal order

完全排除Loop中的Sticky帖子

如果您在滑块中使用粘性帖子,则有时您可能希望从循环中完全排除粘性帖子。您需要做的就是编辑自定义循环以匹配此:

  <?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();  ?>  

此代码不会在post循环中显示任何粘性帖子。有关修改WordPress主题的更多提示,请查看我们的WordPress主题备忘单,供初学者使用。

粘贴帖子是突出您的精选内容的好方法。但是,您的网站上有一些地方您不需要粘贴帖子。 WordPress无法猜测这一点,因此您需要明确告诉WordPress从自定义循环中排除粘性帖子。在本文中,我们将向您展示如何在WordPress中完全排除循环中的粘性帖子,我们还将向您展示如何删除帖子的粘性功能,因此它仍以自然顺序显示。

如何剥夺帖子的粘性能力

当您在选项卡中显示最新帖子时,您不希望粘性帖子保持粘性。如果您不删除粘性功能,最近的帖子区域将无用,因为所有粘性帖子都会占用此区域。这是哪里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();  ?>  

此代码忽略帖子是粘滞的,并按正常顺序显示帖子。使用此代码,您的粘贴帖子将显示在循环中,但它们不会放在顶部。

Sticky posts displayed in normal order

完全排除Loop中的Sticky帖子

如果您在滑块中使用粘性帖子,则有时您可能希望从循环中完全排除粘性帖子。您需要做的就是编辑自定义循环以匹配此:

  <?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();  ?>  

此代码不会在post循环中显示任何粘性帖子。有关修改WordPress主题的更多提示,请查看我们的WordPress主题备忘单,供初学者使用。

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-10 13:52:06
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: