【问题标题】:Wordpress: Removing posts in "The Loop" using filtersWordpress:使用过滤器删除“循环”中的帖子
【发布时间】:2010-12-03 20:46:52
【问题描述】:

阅读 Wordpress 文档后,我意识到您可以使用“循环”内的过滤器从索引中删除帖子,例如:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<!-- The following tests if the current post is in category 3. -->
<?php if (in_category('3')) continue; ?>

<!-- display normal post -->
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>

我想知道是否有一个过滤器/钩子可以在不修改模板的情况下过滤have_posts() 中的帖子。到目前为止,我找到了更改结果的选项,但没有从结果集中删除结果。

【问题讨论】:

    标签: wordpress filter hook


    【解决方案1】:

    您可以连接到“pre_get_posts”。更多信息请点击此处:http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts。如文章所述:

    在创建查询变量对象之后,但在实际查询运行之前调用此钩子。

    使用 query_posts(),您可以覆盖查询变量并排除任何帖子。该链接有一些关于如何执行此操作的不错示例。

    【讨论】:

      【解决方案2】:

      所以您想将其从显示中删除,但仍保留它以防您决定稍后显示它?不太清楚你想做什么。

      举个例子,过去我曾使用查询帖子将类别排除在我的主页之外:http://codex.wordpress.org/Template_Tags/query_posts#Exclude_Categories_From_Your_Home_Page

      【讨论】:

      • 必须同意 Adam 的观点——使用其中一个类别参数(可以在 Adam 链接的那篇文章中找到)和 query_posts 来过滤您的类别。
      猜你喜欢
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多