Support » Fixing WordPress » Exclude category name from the get_the_category function

  • Hi, please I need your help.

    I created a featured content section, and I use a “featured” category to filter the posts to be shown in it. But in the post loop, I need to display the category name, but, I do not want to show the “featured” category name, I need to show only the others categories names, excluding that one.

    Can anyone help me? I has already ready the http://codex.wordpress.org/Function_Reference/get_the_category

    but can’t find the solution.

    Many thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • There are a number of ways to do this. One way is to add this function to your functions.php file, shown below:

    <?php
    function excludeCat($query) {
      if ( $query->is_home ) {
        $query->set('cat', '-3,-5,-23');
      }
      return $query;
    }
    add_filter('pre_get_posts', 'excludeCat');
    ?>

    3, 5, and 23 are the IDs of the categories and the – makes them go away. Hope this helps!

    Thread Starter almeidacbv

    (@almeidacbv)

    PressTrends many thanks for your fast reply, please, how can I use the name of the category rather than the id? Using the name I think turn my theme more global.

    Thread Starter almeidacbv

    (@almeidacbv)

    Your function is realy great, but I can’t exlude the posts from the “Featured” category, I only want to didn’t show the “featured” category description name at the list. A sample is: to turn featured a post categorized as “News”, I need to mark it in “Featured” category too, but at the listing I want to display only the category name “News” near the title of the post.

    Excuse my bad english.

    Ah, I get it now. This snippet below will retrieve all the categories for the post, look or the “Featured” category and remove it from the list of categories for the post. It will only push out other categories besides “Featured”. This goes inside your post loop where you are displaying categories:

    <?php
    //exclude these from displaying
    $exclude = array("Featured");
    
    //set up an empty categorystring
    $catagorystring = '';
    
    //loop through the categories for this post
    foreach((get_the_category()) as $category)
    {
    	//if not in the exclude array
    	if (!in_array($category->cat_name, $exclude))
    	{
    		//add category with link to categorystring
    		$catagorystring .= '<a href="'.get_bloginfo(url).get_option('category_base').'/'.$category->slug.'">'.$category->name.'</a>, ';
    	}
    }
    
    //strip off last comma (and space) and display
    echo substr($catagorystring, 0, strrpos($catagorystring, ','));
    ?>

    Hope this helps!

    Thread Starter almeidacbv

    (@almeidacbv)

    PressTrends works great!!!! that’s it!! Many thanks!

    But I have only one more question, if I want to display only one category name independent if the post was categorized at many categories. What I do?

    Thanks man!!

    You could try adding a limit variable to the snippet. So $g would start at 0 and then hit 1 when the first category is displayed, then stop after that. The revised snippet is town below:

    <?php
    //exclude these from displaying
    $exclude = array("Featured");
    
    // Set initial counter to limit display of only one category
    $g = 0;
    
    //set up an empty categorystring
    $catagorystring = '';
    
    //loop through the categories for this post
    foreach((get_the_category()) as $category)
    {
    	//if not in the exclude array
    	if (!in_array($category->cat_name, $exclude) && $g < 2)
    	{
    		//add category with link to categorystring
    		$catagorystring .= '<a href="'.get_bloginfo(url).get_option('category_base').'/'.$category->slug.'">'.$category->name.'</a>, ';
    
                    // Add to counter after category loop
                    $g++:
    	}
    }
    
    //strip off last comma (and space) and display
    echo substr($catagorystring, 0, strrpos($catagorystring, ','));
    ?>

    Hope this helps!

    Thread Starter almeidacbv

    (@almeidacbv)

    Retrieves an error at this line below:

    [code] $g++: [code]

    Sorry, replace $g++: with $g++;

    Thread Starter almeidacbv

    (@almeidacbv)

    NOW WORKS FINE!!!!

    Many thanks my brother!!!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude category name from the get_the_category function’ is closed to new replies.