我有一个自定义循环,可以根据自定义字段显示帖子。我的问题是,如果我有2篇帖子,并且从循环中排除1篇,那么类别帖子计数(启用显示计数的类别列表小部件)仍然保持为2。
如何将帖子从类别帖子计数中排除
2 个回复
SO网友:Cas Dekkers
假设您有一个包含要排除的帖子ID的列表,可以从(PHP 7.3)开始:
function exclude_posts_from_category( array $cat_args ): array {
$exclude = array(); // TODO Add post IDs to exclude.
$cat_args[\'exclude\'] = $exclude;
return $cat_args;
}
add_filter( \'widget_categories_args\', \'exclude_posts_from_category\', 10, 1 );
您可以阅读widget_categories_args
滤器here. 此外,您可以更改$cat_args
使用参数组合的数组,这些参数可以在中找到the documentation of the wp_list_categories()
function.我没有完全测试这段代码,所以如果您有任何问题,请告诉我。
SO网友:Diogo Gomes
如果要从所有查询中排除帖子,如果您不是“管理员”,并排除“单个”查询-以便打开帖子,可以使用以下方法:
add_action(\'pre_get_posts\', \'exclude_posts_from_all_queries\');
function exclude_posts_from_all_queries($query) {
$exclude_posts = array(\'9\');
if ( !is_admin() && !is_single() ) {
$query->set(\'post__not_in\', $exclude_posts);
}
}
只需将Posts ID号添加到$exclude\\u Posts数组中即可。希望有帮助,干杯
结束