WordPress Wednesday: Thesis Theme Tag Text

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.


There are 5 comments on this post

  1. Wow, I am not at all familiar with switch and case. I’m sure that will come in handy sometime.

    Would I be busting a bubble if I pointed out has_tag()?

  2. You won’t be bursting my bubble, Kristarella.

    Yes, you could use the has_tag() function instead of the switch() statement. It’s really a rock and a hard place issue. If you use the switch() statement like I did, then you’re looping through every tag every time a post is displayed. If you use the has_tag() function then you have to execute an extra if() for each tag you want to target. And I’m not 100% certain because I haven’t really read through the entire WordPress core code, but I’m guessing that calling has_tag() for each tag you want to check is — unless you are only doing it once — going to require more server resources / database requests.

    Using the switch() is a single database query. Using the if ( has_tag( ‘ tag’ ) ) method will require a separate query for each tag you want to target.

    Now … I could be wrong about this because has_tag() might simply be checking a post class object which has already done the query, but I still think you’re going to incur a greater processing cost that way.

    For a low-traffic blog the difference is probably irrelevant, though.

  3. Cool cool. Yeah, I’m really not sure. To be honest I feel like if someone uses Thesis then they can’t be too fussed about PHP requests to the server because the entire thing is massively PHP, more so than previous WP themes. You’re explanation is perfectly reasonable though.

  4. […] WordPress Wednesday: Thesis Theme Tag Text […]

  5. […] Use Thesis to Display Text associated with Tag Author […]

Add to the discussion:

I'll never share your email address and it won't be published.

What Is This?

davidgagne.net is the personal weblog of me, David Vincent Gagne. I've been publishing here since 1999, which makes this one of the oldest continuously-updated websites on the Internet.

bartender.live

A few years ago I was trying to determine what cocktails I could make with the alcohol I had at home. I searched the App Store but couldn't find an app that would let me do that, so I built one.

Hemingway

You can read dozens of essays and articles and find hundreds of links to other sites with stories and information about Ernest Hemingway in The Hemingway Collection.