Forums

[resolved] Excluding categories in the_category - with umlauts (7 posts)

  1. SamiBerlin
    Member
    Posted 3 years ago #

    hey everybody!
    to exclude a specific category at the "the_category" tag you can use this piece of code in your functions.php.

    function the_category_filter($thelist,$separator=' ') {
    	if(!defined('WP_ADMIN')) {
    		//list the category names to exclude
    		$exclude = array('Something','Something Else','Blah','YAY');
    		$cats = explode($separator,$thelist);
    		$newlist = array();
    		foreach($cats as $cat) {
    			$catname = trim(strip_tags($cat));
    			if(!in_array($catname,$exclude))
    				$newlist[] = $cat;
    		}
    		return implode($separator,$newlist);
    	} else
    		return $thelist;
    }
    add_filter('the_category','the_category_filter',10,2);

    (this ist from: http://wordpress.org/support/topic/140469)
    It works good.

    BUT:
    What should I do when I have categories with umlauts, for example "ü" "Ö" "ß"...
    The part "$exclude = array('Something','Something Else','Blah','YAY');" just accepts the real names...
    is there a possibility to use category-slugs or category-ids?

    Thank you so much!!

  2. Otto
    Tech Ninja
    Posted 3 years ago #

    The part "$exclude = array('Something','Something Else','Blah','YAY');" just accepts the real names...

    I don't understand what that means, exactly... PHP is not character-set specific. Just put the umlauts in there. It should work.

    You may need to use the html entities for them instead, if they are entered that way into your database.

  3. SamiBerlin
    Member
    Posted 3 years ago #

    Ok thanks for your help.
    Problem is solved:

    If you are using umlauts you have to define the category with html entities in the wp-admin-area!
    Then this exclude-function works.

  4. fuzzydave
    Member
    Posted 3 years ago #

    I tried adding this function to the functions.php file in wp-includes

    but the site doesnt work and the error is in the logs

    PHP Fatal error: Call to undefined function: add_filter()

  5. Otto
    Tech Ninja
    Posted 3 years ago #

    Don't add anything to the functions.php file in wp-includes. You broke your site by doing that.

    He was referring to the functions.php file in your theme.

  6. clickedcreative
    Member
    Posted 2 years ago #

    Try putting this in your themes functions.php

    function the_category_exclude($separator=', ',$exclude='') {
    	$toexclude = explode(",", $exclude);
    	$newlist = array();
    	foreach((get_the_category()) as $category) {
    		if(!in_array($category->category_nicename,$toexclude)){
    			//$newlist[] = $category->cat_name;
    			$newlist[] = '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>'  . $category->name.'</a>';
    		}
    	}
    	return implode($separator,$newlist);
    }

    Then to use it call the function with the separator and the slugs of the categories to exclude:
    echo the_category_exclude(", ","a-slug,another-slug");

    This is a bit of a hybrid of the code on this page and here:
    http://www.technokinetics.com/exclude-categories-from-the_category/

    Hope this helps someone!

  7. john.andrews
    Member
    Posted 2 years ago #

    ...and yet another approach.

    If you'd like to exclude categories from a returned categories array, try the following. This example uses get_the_category, and provides the advantage of selective exclusion, rather than applying a global filter to the get_the_category function.

    Insert this function into your functions.php:

    function exclude_categories($args) {
    	$count = 0;
    	foreach ($args['categories'] as $category) {
    		if(strpos($args['exclude'],$category->cat_ID)){
    			unset($args['categories'][$count]);
    		}
    		$count++;
    	}
    	$categories = $args['categories'];
    	return array_values($categories);
    }

    Get the categories for a given post id:

    $categories = get_the_category($post->ID);

    Call the exclude function using the cat id's for the exclude argument:

    $args = array(
    	'categories' => $categories,
    	'exclude'    => '30,31,32'
    );
    $categories = exclude_categories($args);

    The result will be the same array as get_the_category provides, with the excludes removed of course. I suspect there are other WP category functions that this will work for as well, but I've yet to test.

Topic Closed

This topic has been closed to new replies.

About this Topic