admin管理员组文章数量:1323225
I have a plugin that create my website sitemap. It starts by getting all categories with get_categories
and then gets all pages for each.
However I have one category that must not be included then I did that in the plugin code:
$args['exclude']=2;
$cats = get_categories( $args );
The problem is that if the plugin replace the file my modiofication will be deleted, I tried with that:
function exclude_cat($taxonomy, $args) {
$args['exclude']=2;
return $args;
}
add_filter('get_categories_taxonomy', 'exclude_cat', 10, 2);
But then no category is returned at all, I think I'm not using it correctly but I don't find examples.
I have a plugin that create my website sitemap. It starts by getting all categories with get_categories
and then gets all pages for each.
However I have one category that must not be included then I did that in the plugin code:
$args['exclude']=2;
$cats = get_categories( $args );
The problem is that if the plugin replace the file my modiofication will be deleted, I tried with that:
function exclude_cat($taxonomy, $args) {
$args['exclude']=2;
return $args;
}
add_filter('get_categories_taxonomy', 'exclude_cat', 10, 2);
But then no category is returned at all, I think I'm not using it correctly but I don't find examples.
Share Improve this question asked Oct 6, 2020 at 6:38 EntretoizeEntretoize 1331 silver badge6 bronze badges1 Answer
Reset to default 0You'll find an appropriate filter in the get_terms
function which is called by get_categories
.
See source code https://github/WordPress/WordPress/blob/master/wp-includes/taxonomy.php#L1247
We don't use any of the callback arguments in this case. We can use array_filter
on the list of terms (WP_Term) objects. See https://developer.wordpress/reference/classes/wp_term/
This allows us to remove a term with a particular ID
add_filter( 'get_terms', function( $terms, $taxonomies, $args, $term_query ){
return array_filter($terms, function( $term ) {
return $term->term_id !== 2;
});
}, 10, 4 );
Note that get_categories
by default only returns categories which have posts assigned, you can change that by adding hide_empty => false
to the $args
array.
本文标签: categoriesHow to exclude a category returned by getcategories from functionphp
版权声明:本文标题:categories - How to exclude a category returned by get_categories from function.php? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742078861a2419573.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论