Forums

the_category as title exclude category (6 posts)

  1. gbaka
    Member
    Posted 3 years ago #

    I'm using the the_category as a title for my post for the front page and I'm wondering if theres a way to exclude a category from the title because if i have more than 1 category it adds more to the title, or if anyone knows a alternative way to display the category name as the title. only one category displays on the front page, i need a to exclude that category so it shows only the only category i include with it.

  2. mhuntdesign
    Member
    Posted 2 years ago #

    I'm having the same issue on the homepage of a site i am building.. There is a module called "Advanced Category Excluder" But it seems like overkill for something simple like this that can be handled with some php code..

  3. Cristiano
    Member
    Posted 2 years ago #

    I'm having the same issue... any help???

  4. makeshift67
    Member
    Posted 2 years ago #

    Here, i found this on google.

    <?php
    //edit below for categories you want excluded
    $exclude = array("Featured", "Uncategorized");
    //how do you want the list separated? just a space is okay
    $separator = " | ";
    //don't edit below here!
    $new_the_category = '';
    foreach((get_the_category()) as $category){
    	if (!in_array($category->cat_name, $exclude)){
    		$new_the_category .= '<a href="'.get_bloginfo(url).'/'.get_option('category_base').'/'.$category->slug.'">'.$category->name.'</a>'.$separator;
    	}
    }
    echo substr($new_the_category, 0, strrpos($new_the_category, $separator));
    ?>

    hope it helps.

  5. schmaart
    Member
    Posted 2 years ago #

    awesome. saved my bacon.

  6. kalligator
    Member
    Posted 2 years ago #

    A nicer alternative building upon http://wordpress.org/support/topic/140469 but using category IDs instead of names, can be found here: http://www.smashingthemes.com/blog/how-to-remove-categories-from-the_category-function-in-wordpress/

    function the_category_filter($thelist,$separator=' ') {
        if(!defined('WP_ADMIN')) {
            //Category IDs to exclude
            $exclude = array(1,5);  
    
            $exclude2 = array();
            foreach($exclude as $c) {
                $exclude2[] = get_cat_name($c);
            }  
    
            $cats = explode($separator,$thelist);
            $newlist = array();
            foreach($cats as $cat) {
                $catname = trim(strip_tags($cat));
                if(!in_array($catname,$exclude2))
                    $newlist[] = $cat;
            }
            return implode($separator,$newlist);
        } else {
            return $thelist;
        }
    }
    add_filter('the_category','the_category_filter', 10, 2);

Topic Closed

This topic has been closed to new replies.

About this Topic