Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

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 "'.$tag.'" - '; } elseif (is_archive()) { wp_title(''); echo ' Archive - '; } elseif (is_search()) { echo 'Search for "'.wp_specialchars($s).'" - '; } 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 "'.$tag.'" - ';

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

echo 'Tag Archive for "'.single_tag_title().'" - ';

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: Thanks to Jeff at ivany.org for pointing out a couple of errors and providing a correct, cleaner version of the code.
<title><?php if (function_exists('is_tag') && is_tag()) { single_tag_title("Tag Archive for ""); echo'" - '; } elseif (is_archive()) { wp_title(''); echo ' Archive - '; } elseif (is_search()) { echo 'Search for "'.wp_specialchars($s).'" - '; } 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> element of your header.php theme 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.

About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
SAC Pro: Unlimited chats.

21 responses to “Perfect WordPress Title Tags Redux”

  1. Hey Jeff, can’t hold back, but your stuff is once again incredibly interesting to read. And congrats on the css gallery!

  2. Jeff Starr 2008/12/10 3:50 pm

    Thanks Simon! Always great to hear from you! Happy Holidays! :)

  3. very intersting article man, so even if we have allinone or platinumseo can we still benefit by bunging this in the header.php.

    Also would it matter if you add it on the end pt top of the header?

  4. When I tried to use your snippet, I ended up without any output. Turns out the $paged variable isn’t defined in my theme. If I remove that lat check, everything works fine. Is $paged supposed to be available to themes or is there something else missing?

    Great site too. I’ve been reading most of the morning!

  5. More fine stuff there, Jeff — BUT. Seasons Greetings notwithstanding, I hereby head-butt you with this question: what’s perfect about a bunch of pages whose titles all begin with the same phrase?

    Surely one should strive to ensure that every page title begins with the most specific and relevant ‘keywordy’ text for that page?

    Hoo’s aboot: ‘Pink Floyd’ – Page 1 : Tag Archives at My Glorious Site’ or similar?

    Just shuffle the cards a bit. Whaddaya think?

    Happy holidays to you and yours, and keep up the quality we’ve come to know and love!

  6. Jeff Starr 2008/12/11 4:42 pm

    @CHTRC Webmaster: Very kind of you to Digg it! Thank you!! :)

  7. I figured out the problems with the $paged code. Missing period to join the string. I also noticed that your use of single_tag_title is incorrect. By default it echos the output instead of returning a string. Here is a cleaner version of your title tags redux that can be cut and paste into a blog.

    <title><?php if (function_exists('is_tag') && is_tag()) { single_tag_title("Tag Archive for ""); echo'" - '; } elseif (is_archive()) { wp_title(''); echo ' Archive - '; } elseif (is_search()) { echo 'Search for "'.wp_specialchars($s).'" - '; } 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>

    HTML encoded version just in case this comment system chomps the tags all up:

    <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>

  8. @Donace: As a matter of fact, I have AiOSEO and continue to use this code for the title tag. When AiOSEO is working, the title tag is overwritten; however, the hard-coded title tag works great for pages not covered by the plugin and also in the event that it should fail. There may be a slight performance hit because of the processing, though; so keep that in mind when considering using it. Frankly, I would be surprised if Louis didn’t criticize me for this strategy! ;)

    @Jeff M: Excellent points, of course — I highly encourage users to “mix” things up according to their needs, SEO skillz, and personal preferences — the more creative, the better! The best part of this code is that it is pretty easy to customize the output by simply rearranging and editing a few things. Many thanks for the idea — the improvement will definitely be integrated into the next related post!

    @Jeff: Ahh, good catch — thanks for pointing that out and even providing an improved version of the code. I am updating the article with your version and dropping a credit link to your site as well. Thanks again for the help, it is greatly appreciated! :)

  9. @Jeff Starr: Thanks so much for the link! You did all the real work, the least I could do is help fix a little typo. :)

  10. Jeff Starr 2008/12/17 5:13 pm

    @Jeff: I look at it as much more than that — you have helped to improve the overall quality and accuracy of the information presented in the article. Crediting you with a link is the least I could do! ;)

  11. OK, you may have now nice s, but there should be a one to one relationship with your ,,s.

    I.e., your should do

    $a=...; $b=...; $c=...;
    $b - $c $a
    $c
    $a
    $b

    I just randomly typed the a b c’s above. My point is and ‘s need to be part of one coordinated “gestalt”.

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
SAC Pro: Unlimited chats.
Thoughts
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Crazy that we’re almost halfway thru 2024.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.