Perfect WordPress Title Tags Redux

In my previous article on WordPress title tags, How to Generate Perfect WordPress Title Tags without a Plugin, We explore everything needed to create perfect titles for your WordPress-powered site. After discussing the functionality and implementation of various code examples, the article concludes with a “perfect” title-tag script that covers all the bases. Or so I thought..

Some time after the article had been posted, Mat8iou chimed in with a couple of ways to improve thie script by cleaning up tag names and specifying page numbers for archive views. Apparently, by replacing the $tag variable with WordPress’ built-in single_tag_title();, titles for Tag-Archive page views will display the tag’s “pretty” name rather than the unformatted version. For example, the tag for Pink Floyd will be displayed correctly as “Pink Floyd” rather than the less friendly “pink-floyd”. And so on.

Mat8iou also suggests appending the page number to titles for the various types of paged archive views: date archives, category archives, author archives, and other types of page views. Without page numbers, page views generate duplicate titles for archives with multiple pages; for example:

  • Tag Archive for 'Pink Floyd' (title for first archive page)
  • Tag Archive for 'Pink Floyd' (title for second archive page)
  • Tag Archive for 'Pink Floyd' (title for third archive page)

..and so on. Such duplicate titles are far from being SEO-friendly, and therefore should be modified to include the associated page number for each page; for example:

  • Tag Archive for 'Pink Floyd' - Page 1
  • Tag Archive for 'Pink Floyd' - Page 2
  • Tag Archive for 'Pink Floyd' - Page 3

Much better! Archived pages with numerically defined titles improve usability and increase the associated SEO value of every page. Now let’s integrate both of these functional improvements into our perfect title script.

Perfect WordPress Title Tags Redux

First, here is our perfect title script as presented in the previous article:

<title><?php if (function_exists('is_tag') && is_tag()) { echo 'Tag Archive for &quot;'.$tag.'&quot; - '; } elseif (is_archive()) { wp_title(''); echo ' Archive - '; } elseif (is_search()) { echo 'Search for &quot;'.wp_specialchars($s).'&quot; - '; } elseif (!(is_404()) && (is_single()) || (is_page())) { wp_title(''); echo ' - '; } elseif (is_404()) { echo 'Not Found - '; } if (is_home()) { bloginfo('name'); echo ' - '; bloginfo('description'); } else { bloginfo('name'); } ?></title>

As discussed above, the first improvement we would like to make to this script involves “cleaner” tag titles. Currently, we are using this snippet to generate the tag portion of the title:

echo 'Tag Archive for &quot;'.$tag.'&quot; - ';

Let’s replace the $tag variable with WordPress’ built-in function, single_tag_title();:

echo 'Tag Archive for &quot;'.single_tag_title().'&quot; - ';

Next, we want to implement page numbering for multiple-page archive views. To do this, we simply add an additional if() clause to the end of the script:

if ($paged>1) { echo '- page ' $paged; }

..which will produce page numbers with the following format:

Tag Archive for 'Pink Floyd' - Page 1

Finally, let’s integrate these new code snippets into our existing script to produce the new-&-improved, even-more-perfect WordPress title script: <update> (12/15/2008): Thanks to Jeff at ivany.org for pointing out a couple of errors and even providing a correct, cleaner version of the code, which is now presented here:</update>

<title><?php if (function_exists('is_tag') && is_tag()) { single_tag_title("Tag Archive for &quot;"); echo'&quot; - '; } elseif (is_archive()) { wp_title(''); echo ' Archive - '; } elseif (is_search()) { echo 'Search for &quot;'.wp_specialchars($s).'&quot; - '; } elseif (!(is_404()) && (is_single()) || (is_page())) { wp_title(''); echo ' - '; } elseif (is_404()) { echo 'Not Found - '; } if (is_home()) { bloginfo('name'); echo ' - '; bloginfo('description'); } else { bloginfo('name'); } ?><?php if ($paged>1) { echo ' - page '. $paged; } ?></title>

Just slap that puppy into the <head> of your header.php WordPress template and enjoy the results: optimized page titles for your entire WordPress-powered site!

For more help with this method, please refer to my previous article on this topic, How to Generate Perfect WordPress Title Tags without a Plugin. You may also benefit from visiting the wp_title section of the WordPress Codex.