Question

this is a follow up question to this post Exclude a category from the filed under list

The solution given works for me. However, I'm curious how I can get this to work conditionally? Specifically, only on category archives? E.g. It will still say "Filed Under: A, B, X" on single post templates. But it will say "Filed Under: A, B" on archives.

Working in Genesis, if that helps.

Was it helpful?

Solution

You can add conditional statements before applying the logic to modify the categories. For example, I added a check that will bail and return the unmodified list of categories if we're not on the category archive page:

// Add the filter to 'the_category' tag.
add_filter( 'the_category', 'the_category_filter', 10, 2 );
function the_category_filter( $thelist, $separator = ' ' ) {

    // Bail if this is not the category archive page.
    // https://developer.wordpress.org/reference/functions/is_tax/
    if ( ! is_tax( 'category' ) ) {
        return $thelist;
    }

    // list the IDs of the categories to exclude
    $exclude = array(4,5);
    // create an empty array
    $exclude2 = array();

    // loop through the excluded IDs and get their actual names
    foreach($exclude as $c) {
             // store the names in the second array
             $exclude2[] = get_cat_name($c);
    }

    // get the list of categories for the current post
    $cats = explode($separator,$thelist);
    // create another empty array
    $newlist = array();

    foreach($cats as $cat) {
        // remove the tags from each category
        $catname = trim(strip_tags($cat));

        // check against the excluded categories
        if(!in_array($catname,$exclude2))

        // if not in that list, add to the new array
        $newlist[] = $cat;
    }

    // return the new, shortened list
    return implode( $separator, $newlist );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top