One of the things I wanted to accomplish with the Betaflow redesign was a better way to manage my Technorati tags. Previously I had been using a combination of plugins that, albeit worked, didn’t work the way I wanted them to.
For this redesign I set aside a portion of each post dedicated to Technorati Tags (see the bottom of the post, “Tags:”). This article will discuss a small piece of code I have added to my index.php to support the addition of Technorati tags via the custom fields portion of each post.
Once you have determined an area on screen for your Technorati Tags you need to first initiate a check to determine if the current post has any tags associated with it. For Betaflow I am using a custom field named “tags,” but you can use whatever you want. WordPress has made it easy to check custom fields with the get_post_meta function, so we use it to check for a tags field and if one does not exist echo “N/A.”
<?php
if (get_post_meta($post->ID, 'tags', true)) {
} else {
echo "N/A";
}
?>
The get_post_meta() function simply asks for a post ID, the name of the field, and a boolean as to whether you want the value returned as a string (true) or array (false).
Now that we’re determining which posts have Technorati Tags, all we have to do is display those tags for each post. We do so by adding the following portion of code:
<?php
if (get_post_meta($post->ID, 'tags', true)) {
echo get_post_meta($post->ID, 'tags', true);
} else {
echo "N/A";
}
?>
Simple, but not really effective (since Technorati looks for the rel=”tag” statement within anchor tags). We need to explode our string of comma-delimited tags, add in the anchor tags for each, and then put it all back together and echo it back out.
Since our field (named “tags”) is a comma delimited list, we can explode that string by the characters “, ” (a comma, then a space). This will create an array with nothing but our tags as each value. Then, we need to iterate through each value and add the opening anchor tag before the value and a closing anchor tag after the value. We’re also going to put a “, ” (a commaa, then a space) after each closing anchor tag, for aesthetics.
Finally, we’ll need to echo that new string back out to the page so our visitors (and more importantly Technorati’s spider) see those tags. Here’s the final bit of code, to complete the whole process:
<?php
if (get_post_meta($post->ID, 'tags', true)) {
$tags_field = get_post_meta($post->ID, 'tags', true);
$tags = explode(", ", $tags_field);
foreach ($tags as $tag) {
$tag_link .= "<a href=\"http://www.technorati.com/tag/" . $tag . "\" rel=\"tag\">" . $tag . "</a>, ";
}
echo substr($tag_link, 0, strlen($tag_link) - 2);
} else {
echo "N/A";
}
?>
You might be asking why our echo is using the substr() function. This is because we are adding a “, ” (a comma, then a space) after every field of our exploded array – including the last one. We need to pop the last two characters off of the end of our string for aesthetic appeal, otherwise we’ll be looking at a list that looks like this: tag, tag2, tag3,
This is very simple to add to your very own WordPress blog and allows for more control over how your Technorati Tags are displayed on page than many of the plugins that have been released thus far. If you have any questions, comments, or ways to optimize this code a bit – feel free to leave a comment.