WordPress.org

Ready to get started?Download WordPress

Forums

Exclude a category from the display of the_category... big, usefull, old problem (14 posts)

  1. Pixelboss
    Member
    Posted 7 years ago #

    I can't find somthing about that or it doesn't work :

    http://wordpress.org/support/topic/49918?replies=4
    http://wordpress.org/support/topic/72421?replies=2

    Is there a solution ? I use a "secret" category to manage my posts and it appears (!!!) when I use the_category tag. It is really (really) ugly.
    Thanks in advance for any help or advise

  2. Pixelboss
    Member
    Posted 7 years ago #

    Is it obvious or there is no answer ?

  3. Pixelboss
    Member
    Posted 7 years ago #

    May someone explain to me why I'm so dummy with this question ? Must I upgrade ? I'm on WP203 but for each urgrade I have 2 weeks of work because of plugins etc... so I'm waiting big holidays at the end of the year... I'm looking outside by the window and I see the sun. I should see the snow in this season, but thanks to cars, there is sun everywhere... Scary...

  4. thedesigncoalition
    Member
    Posted 7 years ago #

    Yeah, I am looking for this too. It's weird, they added so much more options for wp_list_categories but this tag is still so limited.

    I too need to Exclude a couple categories from the_category too. Anybody?

  5. Chris_K
    Member
    Posted 7 years ago #

    Well, the documentation for wp_list_categories is here: http://codex.wordpress.org/Template_Tags/wp_list_categories

    There's a section on include/exclude categories. Wouldn't you start with that?

    Or am I totally misunderstanding the question?

  6. thedesigncoalition
    Member
    Posted 7 years ago #

    Oh I meant that wp_list_categories has so many great options but the_category is still so limited. I need the_category because I am using it in the loop to show which categories a post is filed under and need to exclude a couple. Sorry, re-reading my statement, it didn't make much sense.

  7. Chris_K
    Member
    Posted 7 years ago #

    Ahh...

    Hrmm, that's a bit more of a stumper.

    Off the top of my head, you might want to put a little PHP around the result of the_category and manually do the parse/filtering?

  8. ah642403hy
    Member
    Posted 7 years ago #

    Removing unwanted cats
    replace the the_category function with the code below
    (1234 being the unwanted category.)

    <?php
    foreach((get_the_category()) as $cat) {
    if ($cat->cat_ID != "1324") {
    echo '| <a href="?cat=' . $cat->cat_ID . '">' .
    $cat->cat_name . '</a> ';
    }
    }
    ?>
  9. thedesigncoalition
    Member
    Posted 7 years ago #

    Awesome! It totally works! but one more question, how do you specify multiple categories. for example I want to exclude both category 13 and 14.

    I tried all of the following and it didn't work.

    ...cat_ID != "13,14" 
    
    ...cat_ID != "13","14" 
    
    ...cat_ID != '13','14' 
    
    ...cat_ID != '13,14' 
    
    ...cat_ID != '13&14'

    What's the syntax?

  10. thedesigncoalition
    Member
    Posted 7 years ago #

    Any Ideas ah642403hy or anyone else? ah642403hy's solution works great for 1 category, but how do you specify multiple categories?

  11. thedesigncoalition
    Member
    Posted 7 years ago #

    Duh, I got it.

    <?php
    foreach((get_the_category()) as $cat) {
    if ($cat->cat_ID != '13')
    if ($cat->cat_ID != '14') {
    echo '<a href="?cat=' . $cat->cat_ID . '">' .
    $cat->cat_name . '</a> ';
    }
    }
    ?>
  12. oncidium
    Member
    Posted 7 years ago #

    Would any of this be useful in trying to exclude a category from my feed as I am trying to do in this post: http://wordpress.org/support/topic/124459 Everything I have tried so far doesn't work.

  13. ah642403hy
    Member
    Posted 7 years ago #

    sorry, i should have checked back. another solution for multiple excludes would be by using the 'or'

    if ($cat->cat_ID != '1' || $cat->cat_ID != '2') {

    || = or

  14. afreeorange
    Member
    Posted 7 years ago #

    Here's a little function I wrote after facing a similar problem on my blog. It seems to work for me... I'm sure someone can edit and improve it!

    Here's a sample usage:
    listCatsAndExcludeSome('21,23,67,8', '<span>', '</span>', ', ');

    // List categories and exclude a few
    function listCatsAndExcludeSome($excludedCategories, $before, $after, $middle) {
    
    	// Get list of desired excluded categories
    	$excludedCategoriesList = explode(',', $excludedCategories);
    	$excludedCategoriesListCount = count($excludedCategoriesList);
    
    	// Get list of categories from WordPress
    	$categoryCache = get_the_category();
    	$categoryCacheCount1 = count($categoryCache);
    
    	// Removing unnecessary categories from listing based on passed parameters
    	for ($i=0;$i<$categoryCacheCount1;$i++) {
    		for ($j=0;$j<$excludedCategoriesListCount;$j++) {
    			if ($categoryCache[$i]->cat_ID == $excludedCategoriesList[$j]) {
    				unset($categoryCache[$i]);
    			}
    		}
    	}
    
    	// Re-ordering array-indices and get the size of the array.
    	// NOTE TO SELF: reset($array) has to do with array pointers and means something else!
    	shuffle($categoryCache);
    	$categoryCacheCount2 = count($categoryCache);
    	$k = 1;
    
    	// Now loop through and display the categories
    	foreach ($categoryCache as $category) {
    		echo $before.'<a href="http://YOUR_BLOG_URI/'.$category->category_nicename.'" title="All posts tagged with ‘'.$category->cat_name.'’">'.$category->cat_name.'</a>'.$after;
    		if ($k != $categoryCacheCount2) {
    			echo $middle;
    		}
    		$k++;
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags