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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

get_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