davidgagne.net

  • About
  • Archives
  • Twitter
  • Facebook
  • bartender.live
  • GitHub
  • LinkedIn

WordPress Wednesday: Thesis Theme Tag Text

June 17th, 2009 @ 6:45 am PDT
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.

More posts tagged:
code / hacks / PHP / Programming / themes / thesis / wordpress / wordpresswednesday

More posts categorized:
Programming Web Design

More posts from:
June 2009 / 2009

Post navigation

Previous Post
Previous Post (More-or-Less) Daily David
Next Post
Next Post What about Sicko?

Responses to “WordPress Wednesday: Thesis Theme Tag Text”

  1. June 17th, 2009 @ 7:47 am
    kristarella

    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. June 17th, 2009 @ 8:07 am
    dvg

    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. June 17th, 2009 @ 4:49 pm
    kristarella

    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. June 18th, 2009 @ 12:03 am
    girliegeek

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

  5. June 29th, 2009 @ 8:56 am
    Thesis Tutorial Compilation

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

Comments Closed

New comments are disabled on this post.

Search


Switch to FreshBooks

Switch to FreshBooks

I switched from QuickBooks to FreshBooks in 2015 and am so glad I did. Everything about their billing system is easier.

  • Take Control of Your Invoices

© David Vincent Gagne. All rights reserved. Custom WordPress Theme by Jacket Industries.