admin管理员组文章数量:1122846
I'm trying to exclude a Category from both the main list of Categories in my blog, as well as from the list of categories shown that are assigned to a specific post.
I successfully hid it from the main Category list display with the following code (using 'exclude').
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'exclude' => -2
) );
For the display of each post on my blog feed I have it displaying the Categories as links at the bottom of each content card. And I cannot figure out how to exclude a specific category from this display. Here is the current code:
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );
}
?>
</div>
How do I adjust this second block of code to exclude a category from the display?
I'm trying to exclude a Category from both the main list of Categories in my blog, as well as from the list of categories shown that are assigned to a specific post.
I successfully hid it from the main Category list display with the following code (using 'exclude').
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'exclude' => -2
) );
For the display of each post on my blog feed I have it displaying the Categories as links at the bottom of each content card. And I cannot figure out how to exclude a specific category from this display. Here is the current code:
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
}
echo trim( $output, $separator );
}
?>
</div>
How do I adjust this second block of code to exclude a category from the display?
Share Improve this question asked Aug 20, 2024 at 15:22 jschultzjschultz 132 bronze badges1 Answer
Reset to default 1get_the_category()
has a filter, get_the_categories
, that will filter the categories. You can do something like this:
add_filter( 'get_the_categories', 'wpse426499_filter_categories' );
/**
* Filters the categories.
*
* @param WP_Term[] $categories An array of WP_Term (ie, category) objects.
* @return WP_Term[] The filtered array of WP_Term objects.
*/
function wpse426499_filter_categories( $categories ) {
$undesired_id = 2; // Set this to the ID of the category you want to exclude.
// New array for the category objects.
$filtered = array();
foreach ( $categories as $cat_obj ) {
if ( $cat_obj->term_id != $undesired_id ) {
// If this isn't the unwanted category, add it to the array.
$filtered[] = $cat_obj;
}
}
// Return the array.
return $filtered;
}
You can put this code in the functions.php
file of your active theme, or -- if you're thinking of changing themes at some point -- you can make it into a plugin.
本文标签: Exclude category when displaying all categories assigned to a post
版权声明:本文标题:Exclude category when displaying all categories assigned to a post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736297361a1929995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论