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:
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.
21 responses to “Perfect WordPress Title Tags Redux”
@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.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.
@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! :)
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!
@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..
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; } ?>
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
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 ""); 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 - ';
}
bloginfo('name');
}
?>
<?php if ($paged>1) { echo ' - page '. $paged; } ?>
Regards,
Carlo
Nice Post. It helps me. Thanks for share.