• Resolved nathanielstern

    (@nathanielstern)


    Basically, I’m using a specific/secret category to show all my posts EXCEPT one category, and I’m using this category on my index page. (Think CMS: almost all my posts are in the “artwork” category, as well as several others. Then I have a “press” category for the other posts..) I don’t want “artwork” listed when I call get_the_category from within a post; I just want its other cats listed.

    I found this old thread, which offered a great/simple solution by DSGJohnDoe, but I have to do it in the core code. Basically, I add a hack in the function, like so:

    function get_the_category($id = false) {
    global $post, $category_cache, $blog_id;
    
    	$id = (int) $id;
    	if ( !$id )
    		$id = (int) $post->ID;
    
    	if ( !isset($category_cache[$blog_id][$id]) )
    		update_post_category_cache($id);
    
    	$categories = $category_cache[$blog_id][$id];
    
    	//my hack
    	unset($categories[3]);	
    
    	if ( !empty($categories) )
    		usort($categories, '_get_the_category_usort');
    	else
    		$categories = array();
    
    	return $categories;
    }

    I’d like to find a way to do this as a function or plugin – I’d prefer not to change core code. Someone else offered the following sample of how they excluded categories from a search, but I don’t know how to edit it to work as I want it for get_the_category within a post…

    function myBlogPostsFilter($query)
    {
    global $wp_query;
    
    // I just want to work with searches not all global queries.
    if ($query->is_search)
    {
    // The line below sets/unsets the categories to search
    // To exclude a category just provide the cat_id with
    // a preceeding '-'.
    $query->set('cat','-3,-4,-5,-6,-7');
    }
    return $query;
    }
    add_filter('pre_get_posts','myBlogPostsFilter');

    Any help on combining the two would be greatly appreciated.Thank you!

Viewing 11 replies - 1 through 11 (of 11 total)
  • How about looping through categories and listing only those which are not ‘artwork’… like…

    foreach((get_the_category()) as $cat) {
      if (!($cat->cat_name=='artwork')) echo $cat->cat_name . ' ';
    }

    that’s just off the top of my head, you could neaten that somewhat, but basically it would report every cat name, except the one specified, and could easily be changed to muffle multiple category names.

    I should have added – you would put that somewhere in your template wrapped in php tags, or in your page with a php exec plugin.

    Thread Starter nathanielstern

    (@nathanielstern)

    i made a mistake; i’m calling get_the_category_list which in turn calls get_the_category in a loop, so I’m kinda doing what you say here, but using WP’s built in functions. i could, rather, rename and rewrite these functions in my functions.php, per your suggestion above, and then just exclude my one category this way (or with unset, per the other code above), but I’m wondering if there is not a more efficient solution? if not, so be it. let me know what you think.

    thanks, n

    Thread Starter nathanielstern

    (@nathanielstern)

    Did it!

    OK, I did an uber cheat string replace for the link with “nothing” in my template. My category name is descending, and the category list sits behind the slug “art.” so the call goes as follows:

    <?php echo (str_replace('<a href="'. get_settings('home') .'/art/descending/" title="View all posts in descending" rel="category tag">descending</a>,', '', (get_the_category_list(', ')))); ?>

    Thanks for the help!

    I just found a better solution, combining the two first solutions.
    In a file named functions.php located in my template folder, I copied/pasted the functions get_the_category_list() and get_the_category(), I renamed these functions in get_the_category_list_excludeCat() and get_the_category_excludeCat() with two little changes :

    • changed the function get_the_category into get_the_category_excludeCat() in the function get_the_category_list_excludeCat()
    • excluded a category in the function get_the_category_excludeCat()

    My english is sooo bad, so check the following code, I think it would be more clear.

    function get_the_category_list_excludeCat($separator = '', $parents='') {
    	global $wp_rewrite;
            // change the function get_the_category into get_the_category_excludeCat()
    	$categories = get_the_category_excludeCat();
    	if (empty($categories))
    		return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
    
    	$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
    
    	$thelist = '';
    	if ( '' == $separator ) {
    		$thelist .= '<ul class="post-categories">';
    		foreach ( $categories as $category ) {
    			$thelist .= "\n\t<li>";
    			switch ( strtolower($parents) ) {
    				case 'multiple':
    					if ($category->parent)
    						$thelist .= get_category_parents($category->parent, TRUE);
    					$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a></li>';
    					break;
    				case 'single':
    					$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
    					if ($category->parent)
    						$thelist .= get_category_parents($category->parent, FALSE);
    					$thelist .= $category->name.'</a></li>';
    					break;
    				case '':
    				default:
    					$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
    			}
    		}
    		$thelist .= '</ul>';
    	} else {
    		$i = 0;
    		foreach ( $categories as $category ) {
    			if ( 0 < $i )
    				$thelist .= $separator . ' ';
    			switch ( strtolower($parents) ) {
    				case 'multiple':
    					if ( $category->parent )
    						$thelist .= get_category_parents($category->parent, TRUE);
    					$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->cat_name.'</a>';
    					break;
    				case 'single':
    					$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>';
    					if ( $category->parent )
    						$thelist .= get_category_parents($category->parent, FALSE);
    					$thelist .= "$category->cat_name</a>";
    					break;
    				case '':
    				default:
    					$thelist .= '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . $rel . '>' . $category->name.'</a>';
    			}
    			++$i;
    		}
    	}
    
    	return apply_filters('the_category', $thelist, $separator, $parents);
    }
    
    function get_the_category_excludeCat($id = false) {
    	global $post, $term_cache, $blog_id;
    
    	$id = (int) $id;
    	if ( !$id )
    		$id = (int) $post->ID;
    
    	$categories = get_object_term_cache($id, 'category');
    	if ( false === $categories )
    		$categories = wp_get_object_terms($id, 'category');
    
    	//exclude cat 3 :
    	unset($categories[3]);		
    
    	if ( !empty($categories) )
    		usort($categories, '_usort_terms_by_name');
    	else
    		$categories = array();
    
    	foreach(array_keys($categories) as $key) {
    		_make_cat_compat($categories[$key]);
    	}
    
    	return $categories;
    }

    This works great
    <?php foreach((get_the_category()) as $cat) {
    if (!($cat->cat_name==’artwork’)) echo $cat->cat_name . ‘ ‘;
    } ?>
    Is it possible to exclude multiple categories instead of just one?
    Many thanks
    saved me a lot of trouble here =)

    yes, quite possible.

    <?php
      foreach((get_the_category()) as $cat) {
        if (
          $cat->cat_name!=='one' ||
          $cat->cat_name!=='two' ||
          $cat->cat_name!=='three' ||
          $cat->cat_name!=='four'
        ) echo $cat->cat_name . ' ';
      }
    ?>

    note the lack of the logical ‘or’ || on the last category.

    Thank you for the code!

    Ivovic, that code for multiple categories didn’t work…
    Can someone fix him please? ??

    And by the way, how can I make that tags clickable? (with the link)

    Please, anyone?

    *BUMP* ??

    What is wrong with this code?

    <?php
      foreach((get_the_category()) as $cat) {
        if (
          $cat->cat_name!=='one' ||
          $cat->cat_name!=='two' ||
          $cat->cat_name!=='three' ||
          $cat->cat_name!=='four'
        ) echo $cat->cat_name . ' ';
      }
    ?>

    Why didnt work? ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘exclude a category from get_the_category’ is closed to new replies.