Wordpress:使用过滤器删除“循环”中的帖子

时间:2009-10-05 07:04:26

标签: wordpress filter hook

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

<?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()中的帖子而不修改模板。到目前为止,我找到了更改结果的选项,但没有从结果集中删除结果。

2 个答案:

答案 0 :(得分:4)

你可以勾选'pre_get_posts'。更多信息:http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts。正如文章所描述的那样:

  

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

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

答案 1 :(得分:1)

所以你想要将它从显示中移除但是仍然有它'有'你决定稍后再显示它吗?不清楚你想做什么。

举个例子,过去我使用“查询帖子”来保留我的主页上的类别:http://codex.wordpress.org/Template_Tags/query_posts#Exclude_Categories_From_Your_Home_Page