Jesse J Heap & Son, Inc.
 

Home ยป Excluding tags from Posts in WordPress

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:

function pk_the_tags( $before = ‘Tags: ‘, $sep = ‘, ‘, $after = , $exclude = ) {
        $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:

<?php if (get_the_tags()) pk_the_tags(‘, ‘, ‘, ‘, , ‘featured’); ?>
 

9 Responses to “Excluding tags from Posts in WordPress”

  1. Miriam Says:

    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!

  2. Jesse Says:

    Miriam – you can put it in function.php. That will allow you to call it from any template within your theme.

  3. ed Says:

    Hey, this code looks good, but are you talking about functions.php of Theme Folder or WP-include folder?. I can’t make it works

  4. webmaster Says:

    Ed – functions in the theme folder

  5. Lily Says:

    It does not work in WP 2.9.1..

    Any other solution?

    Thanks :)

    Lily, I’m running this code fine in 2.9.1. There are no version issues that I’m aware of. If you are having issues it may be related to something else (i.e. your theme).

  6. Lily Says:

    I have just tried this function on default theme, and again I got blank page :-)

    Above I put above function in functions.php in theme folder, and later call with code above..

  7. Lily Says:

    LoL :D I wanted to say that I put above function in functions.php in theme folder, and later call it as mentioned.

  8. Lily Says:

    Me again..
    I found solution here
    http://blogandweb.com/wordpress/como-excluir-etiquetas-de-la-lista-de-tags-en-wordpress/

    Code is identical except few quotes :D

  9. schmaart Says:

    Thanks Lily — your reference worked for me…

Leave a Reply