Display tags anywhere in WordPress

Posted by saurabh On August - 4 - 2009
        ('DiggThis)           SocialTwist Tell-a-Friend     
VN:F [1.9.1_1087]
Rating: +4 (from 4 votes)

I tried for nearly 2 hours before I could successfully display tags outside the WordPress loop. If you  have to display tags within the loop, use ‘the_tags()’.  ‘the_tags()’ will display the tags with proper format maintaining their links. But if you have to display tags outside the loop, you need some codes. The link for each tag has to be manually set.

<div class="tags">
<?php

global $id;
$tag_text="";

foreach(get_the_tags($id) as $tag) 
{ $tag_text=$tag_text."<a href=\"http://www.techwhiz.in/tag/".$tag->slug."/\">".$tag->name."</a>, "; }

echo substr($tag_text,0,(strlen($tag_text)-2));

?>
</div>

Just copy the above code and paste it in anywhere outside the loop in your WordPress blog.
Don’t forget to change the link (http://www.techwhiz.in/tag/) before you paste it in your blog.

I have used ‘substr’ function to trim comma after the last tag.

I have tested the above codes in WordPress 2.8

VN:F [1.9.1_1087]
Rating: +4 (from 4 votes)
-->
Related Posts:
  • Bryan
    This is excellent.
blog comments powered by Disqus

There is nothing lost or wasted in this life. — Bhagvad Gita