
For this blog I needed a way to put tags outside of “The Loop” and I couldn’t believe how hard it was. It should work just like categories, but of course it doesn’t because that would be too easy. I found a few forum posts, but I also noticed none of them made links of the tags they simply listed them. Another caveat was a trailing comma at the end of the tags.
I decided to write my own function called global_tags(); Installation is super easy even for a non programmer type. Instructions are below the code and the output is what you see to the right where it says Tags:etc,etc
There is only 1 parameter for now which is what your base category is. For my site it’s “topics”. It’s whatever comes before your actual post names.
I’m calling this a 0.9 as I want to able to detect the base tag, allow you to choose if you want them linked, choose what want as the delimiter and pick what code you want before and after each tag.
<?php function global_tags($cat_base){ /************************************************************ ==Version 0.9== global_tags(); is a WordPress function that lets you display the post tags outside of WP's "The Loop" and makes them in a <a href="/taglink">Tag</a> manner. The only parameter is the base category for your permalinks that you specified is in the settings of your wp-admin Please keep this comment in place! http://kneedeepincode.com/tags-outside-the-loop-in-wordpress simply call by putting global_tags('basetag-here'); *************************************************************/ //grab the post data globally and not through "The Loop" global $post; //Make $the_tags exist $the_tags =''; //Check to make sure there are tags if(get_the_tags($post->ID)){ //Get the tags for this page's id and iterate through them foreach(get_the_tags($post->ID) as $tag) { //Get the tag name $tag_name = $tag->name; //Get the tag url $tag_url = $tag->slug; //get the URL of my blog with category base specified $the_url = get_bloginfo('url').'/'.$cat_base; //Start adding all the linked tags into a single string for the next step $the_tags = $the_tags.'<a href="'.$the_url.'/tag/'.$tag_url.'/">'.$tag_name.'</a>, '; } //Replace the comma which is -2 spaces from the right of the string with nothing (nothing is the 2nd parameter) echo substr_replace($the_tags ,"",-2); } } ?>
==Instructions==
Just copy the code above and paste it into either a functions.php file (I have a functions.php file in my template directory in an includes folder) or somewhere else in your template.
If you put this code in a file that is not the same file you are going to be using global_tags(); (e.g. a functions.php file in your includes directory) then you need to include the file like this (where includes/functions.php is the path to the file you put the code in):
<?php include('includes/functions.php'); ?>
==Update==
Thanks to Nicholas Turbanov of solidangle.fi for catching a bug with the tag names and slugs. Code has been updated and now will work with tags that contain special characters such as spaces.
thank you very much …
this was exactly what i needed, but im a bit confused, how do i use this ?
i put this in my functions.php, so now what ? thanks in advance …
I emailed ya
let me know if that helps.
For everyone else after placing this in a functions.php file just add:
global_tags('topics');. Topics, in this example, is referring to my basetag. You’ll see it in my URL up in the URL bar. Make sure to put that in PHP tags!Thanks for this snippet, it was exactly what I needed. However if I may suggest a small improvement, replace the ‘name’ in
//Get the tag name
$tag_name = $tag->name;
with ’slug’ to keep tags with spaces in their names from breaking the link.
Thanks so much for this. I have been working on this since last night and could not believe how hard it is to grab the tags outside of the loop. You save me a serious headache.
@Nicholas Turbanov: Thanks! I’ve updated the code.
Thanks Oscar, just did a quick search for how to display tags outside the loop and yours came up 4th! Great post, exactly what I needed
Just wanted to say thank you for the code. Didn’t realize it would be ridiculous just to add some tags to the header of my current theme. Works beautifully! Thanks!
That’s pretty cool when your friend accidently finds your web site on google when looking for information
Neither did I! I looked for like over an hour the first time and realized… “wait! there is no default way to do this?!”.
Thanks for this awesome code! It was a great start to just what I needed! Below are some tweaks I made to achieve the following: Display tags from a specific page ID; Made $cat_base not required, in case that was set to nothing in the admin (like mine); Used return at the end instead of echo so it can be inserted into your page code like so… echo ‘Tags: ‘ . global_tags($PageID) . ”;
Of course $PageID would need to be set like $PageID=get_the_ID(); prior to the code above.
Hope this helps!!
function global_tags($ID=”,$cat_base=”)
{
/************************************************************
==Version 0.9==
global_tags(); is a WordPress function that lets you display
the post tags outside of WP’s “The Loop” and makes them in a
Tag manner. The only parameter
is the base category for your permalinks that you specified
is in the settings of your wp-admin
Please keep this comment in place!
http://kneedeepincode.com/tags-outside-the-loop-in-wordpress
simply call by putting global_tags(’basetag-here’);
*************************************************************/
global $post; $the_tags = ”;
if(!$ID)
{ $ID = $post->ID; }
if(get_the_tags($ID))
{
foreach(get_the_tags($ID) as $tag)
{
$tag_name = $tag->name;
$tag_url = $tag->slug;
$the_url = get_bloginfo(’url’);
if($cat_base)
{ $the_url .=’/’ . $cat_base; }
$the_tags .= ‘‘ . $tag_name . ‘, ‘;
}
return substr_replace($the_tags ,”",-2);
}
}