Latest TweetsVerify any search engine or visitor via CLI Forward-Reverse Lookup perishablepress.com/cli-forwar…
Perishable Press

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: 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 &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> 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.

Jeff Starr
About the Author Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
Archives
21 responses
  1. Jeff Starr

    @jidanni: Looks like WordPress gobbled your code! Please repost using <code> tags around each term and individual line. Or, alternately, you may send an email to me (Jeff at the current domain), and I will update your comment accordingly.

  2. Your program stripped what I was trying to say.

    I was trying to say that you should first produce a set of items.

    Then take these items, and display them one way in the HTML TITLE.

    And then take these items, and display them in another way in the HTML H1, H2, and H3 headers.

    My point is the items should only be produced once.

    Currently you just produce them once, for just the HTML TITLE.

    Then later, they apparently will get produced independently again, for the HTML H1, H2, and H3 headers.

  3. Jeff Starr

    @jidanni: Yes, sorry about that — seems like WordPress gets hungry sometimes ;P You make a great point, however, and I would be more than happy to post any code that might help other readers take advantage of your idea (see comment #13). Otherwise, thanks for sharing your ideas with us! :)

  4. I noticed that if I have a static home page in my blog, the title looks like: “- Blogname” (it skips the Bloginfo altogether). And if I change the is_home to is_front_page, it looks like this: “- Blogname – Bloginfo” (note the one extra hyphen in the front, it probably comes from the is_page).

    This is a tad off topic, but, now days what is the role of keywords in search engine optimization? I can probably harness the power of keywords for every page by using a simple custom field?

    Thanks for the nice article!

  5. Jeff Starr

    @Andy: Thanks for the heads up on the title-display for static-page blog configurations. I will definitely be looking into it.

    As for keywords, I wish I could say for sure, but unfortunately I am far from an SEO “expert”. I do know that keywords continue to play a vital role in the indexing and ranking of websites, so I continue to do my best to produce content that takes that into consideration. But then again, there will be those who may disagree..

  6. Hi Jeff, i don’t know why i had to do it like this (wordpress version 2.9):

    WordPress just simply didn’t wanna recognize is_home() and kept putting a (-) infront of my titles. i only got it to work using this way:

    1) { echo ' - page '. $paged; } ?>

  7. Hi Guys,

    I was having the same problem as Andy & Jaysone having the hyphen (-) in front of my “front page”. here is my code that seems to work, please feel free to correct and criticise where necessary:

    1) { echo ' - page '. $paged; } ?>

    Regards,
    Carlo

  8. Sorry, formatting broke… trying again:

    <?php
    if (!(is_home()) && (is_front_page()) || (is_home())) {
    bloginfo('name'); echo " - "; bloginfo('description');
    } else {
    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 - ';
    }
    bloginfo('name');
    }
    ?>
    <?php if ($paged>1) { echo ' - page '. $paged; } ?>

    Regards,
    Carlo

  9. Mahmud Ahsan March 26, 2010 @ 9:37 pm

    Nice Post. It helps me. Thanks for share.

[ Comments are closed for this post ]