Get post tag name by slug or id

Views: 98 Last modified: August 09th, 2011 Comments: 0

With this simple function, designed for wordpress, you can retrieve the post tag name by the post tag ID or slug. Just provide the the ID or slug as the function parameter and the function will return the tag name.

<?php
/**
 * GetTagName()
 * Retrieves the post tag name by the ID or slug
 * @param mixed $meta
 * @return string The TagName
 */
function GetTagName($meta){
    if (is_string($meta) || (is_numeric($meta) && !is_double($meta))
            || is_int($meta)){
                if (is_numeric($meta))
                    $meta = (int)$meta;
                        if (is_int($meta))
                            $TagSlug = get_term_by('id', $meta, 'post_tag');
                        else
                            $TagSlug = get_term_by('slug', $meta, 'post_tag');

                    return $TagSlug->name;
            }
}

// Get by slug
echo GetTagName(get_query_var('tag'));
// Get by ID
echo GetTagName('441');
// Get by slug
echo GetTagName('tag-slug');
?>
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

    Mail this!

    To: From:Sum {5+6} =  
    Anything to add ?

        You must be logged in to post a comment.