WordPress.org

Ready to get started?Download WordPress

Forums

Exclude category name from the get_the_category function (10 posts)

  1. almeidacbv
    Member
    Posted 1 year ago #

    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

  2. PressTrends
    Member
    Posted 1 year ago #

    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!

  3. almeidacbv
    Member
    Posted 1 year ago #

    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.

  4. almeidacbv
    Member
    Posted 1 year ago #

    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.

  5. PressTrends
    Member
    Posted 1 year ago #

    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!

  6. almeidacbv
    Member
    Posted 1 year ago #

    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!!

  7. PressTrends
    Member
    Posted 1 year ago #

    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!

  8. almeidacbv
    Member
    Posted 1 year ago #

    Retrieves an error at this line below:

    [code] $g++: [code]

  9. PressTrends
    Member
    Posted 1 year ago #

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

  10. almeidacbv
    Member
    Posted 1 year ago #

    NOW WORKS FINE!!!!

    Many thanks my brother!!!

Topic Closed

This topic has been closed to new replies.

About this Topic