1
votes

I have a custom post type in which I have custom taxonomies setup.

I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though. Here is my code to output a list of the categories the custom post type is filed under:

<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>

How do I exclude a specific category?

Thanks.

1
Can you post the code you used for registering the custom taxonomy? - Robbie

1 Answers

2
votes

You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.

Give this a try:

function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
    $terms = get_the_terms( $id, $taxonomy );

    if ( is_wp_error( $terms ) )
        return $terms;

    if ( empty( $terms ) )
        return false;

    foreach ( $terms as $term ) {
        if(!in_array($term->term_id,$exclude)) {
            $link = get_term_link( $term, $taxonomy );

            if ( is_wp_error( $link ) )
                return $link;

            $excluded_terms[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
            }
    }

    $excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );

    return $before . join( $sep, $excluded_terms ) . $after;
}

and then use it like this:

<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>