Excluding tags from Posts in Wordpress
If you are using the wordpress the_tags function and you want to exclude certain tags from showing up with associated posts you can use the following function in place of the_tags:
$tags = get_the_tags();
if ( empty( $tags ) )
return false;
$tag_list = $before;
foreach ( $tags as $tag ) {
if (!empty($exclude))
$pos = stripos( $exclude, $tag->name);
else
$pos = false;
if ($pos=== false)
$tag_links[] = ‘<a href="’ . get_tag_link($tag->term_id) . ‘">’ . $tag->name . ‘</a>’;
}
if (empty($tag_links))
return false;
$tag_links = join( $sep, $tag_links );
$tag_links = apply_filters( ‘the_tags’, $tag_links );
$tag_list .= $tag_links;
$tag_list .= $after;
echo $tag_list;
}
So if you want to exclude all tags with the word featured in the tag name you could call it as follows:
March 22nd, 2008 at 2:04 pm
This is a really handy tip, but one thing isn’t clear to me: where does the function code go? Does it go in function.php? Thanks!
April 9th, 2008 at 1:34 pm
Miriam - you can put it in function.php. That will allow you to call it from any template within your theme.