WordPress.org

Forums

exclude categories from showing in the_category (3 posts)

  1. Maxaud
    Member
    Posted 6 years ago #

    How would I go about showing the categories a post belongs to while excluding certain categories from the list? Is there a different template tag that would work better than the_category?

    Thanks in advance!

  2. nbtimmer
    Member
    Posted 6 years ago #

    I faced the same problem and created this function:

    function incomplete_cat_list($separator) {
    	$first_time = 1;
    	foreach((get_the_category()) as $category) {
    		if ($category->cat_name != 'FirstCat' && $category->cat_name != 'SecondCat') {
    			if ($first_time == 1) {
    				echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>'  . $category->name.'</a>';
    				$first_time = 0;
    			} else {
    				echo $separator . '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
    			}
    		}
    	}
    }

    Put it in the functions.php file of your theme. In your template you can use incomplete_cat_list('seperator') instead of the_category('seperator').

    I hope this function helped you!

  3. Maxaud
    Member
    Posted 6 years ago #

    Thanks, I'll fool around with it.
    Much appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic