Wordpress,过滤主页上的消息 [英] Wordpress, filter messages on main page
问题描述
如何在我的网站的主页上隐藏某个类别的帖子和某个 ID?我需要像过滤器这样的解决方案:
How can I hide posts from some category with some ID from main page of my site? I need solution like filter:
function exclude_post($query) {
if ($query->is_home) {
...
}
return $query;
}
add_filter('pre_get_posts', 'exclude_post');
有人能举个例子吗?
推荐答案
Use $query->set( $query_var, $value );
其中 $query_var 是您要添加的变量/在查询中更新.所以把它放在你的条件中:
Use $query->set( $query_var, $value );
where $query_var is the variable you want to add/update in query. So put this inside your condition:
// 1st parameter is the query variable the 2nd is its value, in this case an array of category IDs
$query->set( 'category__not_in', array( 2, 6 ) );
记住对 $query->is_main_query()
进行条件检查是一种很好的做法.pre_get_posts
是一个动作钩子,所以你必须将 add_filter
更改为 add_action
.动作钩子不会返回值,过滤器会.
Remember is good practice put in condition a check to $query->is_main_query()
. pre_get_posts
is an action hook so you have to change add_filter
to add_action
.
An action hook doesn't return a value, a filter does.
示例
function exclude_post( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category__not_in', array( 2, 6 ) );
}
}
add_action('pre_get_posts', 'exclude_post');
更新
根据问题出现的新详细信息,为了排除提要流中但不在类别存档中的某些帖子,条件检查可能如下所示:
According to the new details emerging by the question,in order to exclude some posts in feeds stream, but not in category archive, the conditional check might look like:
if( $query->is_feed() && !$query->is_archive() )
或
if( $query->is_feed() && !$query->is_category() )
希望有帮助!
这篇关于Wordpress,过滤主页上的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!