Today I’m going to explain how to make your WordPress blog display a special note based on a tag associated with a post. This tip is uniquely written for users of Chris Pearson’s awesome Thesis Theme, but if you are crafty enough with WordPress, you’ll be able to use it regardless of the theme you’re using.

WordPressI wrote this function because I was tired of copying and pasting the same text every time I added an entry for my iTunes Tuesday or WordPress Wednesday posts. At the end of every post I included a paragraph to let people know that the post being viewed was part of a series, and I added a note to suggest subscribing to my site’s RSS feed. This usually meant I had to go find a previous post in the series, open it in the editing window, and then copy and paste the text from there into my new post. That, my friends, is a hassle.

The Thesis Theme gives you the ability — through hooks — to optionally execute a function every time a post is displayed. (You can read all about Thesis hooks in the manual, and the thesishooks.com site is an excellent resource for learning where they appear in the theme.)

I did some research in the WordPress documentation and found how to determine which tags are associated with a post. Because every post can have multiple tags it’s not exactly straightforward. You have to loop through the collection of tags to see if the tag you want to target is one of the ones connected to the post, but it can be done with a little elbow grease.

Again: This code will only work in the Thesis Theme. If you are not using the Thesis Theme, you can certainly do the same thing … it just won’t be quite as simple.

Open your custom_functions.php file and add the following:

function tagPostFooter(){
		if ( is_single() ){
			global $post;
			$returnstring = "";
			$the_tags = get_the_tags();
			if ( $the_tags ){
				foreach ( $the_tags as $t ){
					switch( $t->name ){
						case "wordpresswednesday":
							$returnstring .= "<p class=\"alert\">Every Wednesday I do my best to provide a helpful WordPress tip.<br /><a title=\"Get Updates Via RSS\" href=\"http://feeds.feedburner.com/davidgagne\">Subscribe to my feed</a> to stay updated!</p>";
							break;
						case "itunestuesday":
							$returnstring .= "<p class=\"alert\">Every Tuesday I do my best to provide a helpful iTunes tip.<br /><a title=\"Get Updates Via RSS\" href=\"http://feeds.feedburner.com/davidgagne\">Subscribe to my feed</a> to stay updated!</p>";
							break;
						case "":
						default:
							break;
					}
				}
			}
			echo $returnstring;
		}
	}
add_action( 'thesis_hook_after_post', 'tagPostFooter'  );

There you go. The only tricky part is that you must loop through the tags to find the one you want. In this example I’m adding a note to any post tagged as “iTunesTuesday” or “WordPressWednesday”. Obviously you’ll need to replace “wordpresswednesday” and “itunestuesday” with the tags that you want to target. You can have as many or as few case options as you’d like.

And you can see this in action if you scroll down just a smidge.