[wp-hackers] Hide category

DD32 wordpress at dd32.id.au
Wed Dec 3 11:36:50 GMT 2008


IMO: Dont use output buffers, Create a new function in your themes  
function.php and use that instead, Here's one i use:

I have a category 'prefixes' under which i have a myriad of other  
categories (I use them to prefix the title of the post with a category:  
The red part of each title: http://dd32.id.au/blog/), I didnt want those  
categories showing up in the category listing.

You could probably also hook on something like get_terms and use a few  
conditionals (!is_admin() && $taxonomy == 'category') and filter out at  
that stage if you wanted to completely get rid of them.


function theme_the_category($before = '', $sep = '', $after = '') {
	global $wp_rewrite;
	$cats = get_the_category();

	if ( $prefix = theme_get_category_by_slug('prefixes') ) {
		foreach ( $cats as $key => $cat )
			if ( $cat->parent == $prefix->term_id )
				unset($cats[ $key ]);
	}

	$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ?  
'rel="category tag"' : 'rel="category"';
	$display_cats = array();
	foreach ( (array) $cats as $cat )
		$display_cats[] = '<a href="' . get_category_link($cat->term_id) . '"  
title="' .
							sprintf(__('View all posts in %s'), $cat->name) . '" ' . $rel . '>'  
. $cat->name . '</a>';

	if ( ! empty($display_cats) )
		echo $before . implode($sep, $display_cats) .  $after;
}

//Cached wrapper around get_category_by_slug()
function theme_get_category_by_slug($slug) {
	if ( ! $category = wp_cache_get($slug, 'category') ) {
		$category = get_category_by_slug($slug);
		wp_cache_set($slug, $category, 'category');
	}
	return $category;
}

On Wed, 03 Dec 2008 22:26:36 +1100, scribu <scribu at gmail.com> wrote:

> On Tue, Dec 2, 2008 at 6:15 PM, Dan Gayle <dangayle at gmail.com> wrote:
>
>> Here's an example of what I am talking about:
>>        LOOP
>>        The post headline
>>        The the post text
>>        The the post author
>>        The the post date
>>        The post's categories: News, Featured News, Google News <<<---  
>> Need
>> to hide just this
>>        END LOOP
>>
>> I need to hide the Google News category from the display on the blog,
>> without compromising the rest of the post. Thanks for everyone's  
>> patience :)
>>
>> Dan
>
>
> This should work (not tested):
>
>
> Instead of <?php the_category ?>
>
> put
>
> <?php
> ob_start();
> the_category();
> $categories = ob_get_clean();
> str_replace('GoogleNews', '' , $categories);
> echo $categories;
> ?>
>




More information about the wp-hackers mailing list