Automatic SEO Titles for All Your Pages

milotitles1

The title of your page (The few words in the very top bar of your browser window)  is arguably the most important thing search engines look at as they decide where to rank your page.

Most themes come with a very basic title set up; the name of your post and the name of the blog. It works well enough on regular post pages, but what about search pages, archives, 404s and all the others?

And some of these will be much more useful to your readers as well. We all know how helpful it is to open up 6 different tabs on one website only to find that they all start with the site’s name. Doesn’t help you keep track of all the pages you’ve opened very much.

The code below can be copied and pasted directly into your blog to create different titles for all the different types of page.

The New Titles

Below I have listed the new formats the titles will take for each of your pages (The actual title used on the page starts after the – ).

  • Home Page – Blog Name | Blog Description
  • Search Results – Search Results for search terms11 Articles | Blog Name
  • 404 (Error) Page  – Blog Name | 404 Nothing Found
  • Author Archives – Blog Name | Author Archives
  • Single Post – Post Name | Category Name | Blog Name
  • Page – Page Name | Blog Name
  • Category Page – Category Name | Category Description | Blog Name
  • Monthly Archive – Blog Name | Archive | Month, Year
  • Day Archive – Blog Name | Archive | Month Day, Year
  • Tag – Tag Name | Blog name

How to Add To Your Blog

Open your theme’s header.php file and look for the line starting with .

In the default theme, it will look like this:

<title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>

Now delete this line and replace it with the following:

<title> <?php if ( is_home() ) { ?><? bloginfo('name'); ?> | <?php bloginfo('description'); ?><?php } ?>
<?php if ( is_search() ) { ?>Search Results for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); echo $key; _e(' &mdash; '); echo $count . ' '; _e('articles'); wp_reset_query(); ?><?php } ?>
<?php if ( is_404() ) { ?><? bloginfo('name'); ?> | 404 Nothing Found<?php } ?>
<?php if ( is_author() ) { ?><? bloginfo('name'); ?> | Author Archives<?php } ?> 
<?php if ( is_single() ) { ?><?php wp_title(''); ?> | <?php 
$category = get_the_category();
echo $category[0]->cat_name;
?> | <? bloginfo('name'); ?><?php } ?>
<?php if ( is_page() ) { ?><? bloginfo('name'); ?> | <?php 
$category = get_the_category();
echo $category[0]->cat_name;  ?>|<?php wp_title(''); ?><?php } ?> 
<?php if ( is_category() ) { ?><?php single_cat_title(); ?> | <?php $category = get_the_category(); 
echo $category[0]->category_description; ?> | <? bloginfo('name'); ?><?php } ?> 
<?php if ( is_month() ) { ?><? bloginfo('name'); ?> | Archive | <?php the_time('F, Y'); ?><?php } ?>
<?php if ( is_day() ) { ?><? bloginfo('name'); ?> | Archive | <?php the_time('F j, Y'); ?><?php } ?>
<?php if (function_exists('is_tag')) { if ( is_tag() ) { ?><?php single_tag_title("", true); } } ?> | <? bloginfo('name'); ?>
</title>

For anyone who has meddled with their theme’s code a little before, you will see that the code above is (for the most part) fairly simple. You can chop and change as you like if a different style of title would suit your blog better.

What do you do for the titles on your blog? There are some great plugins out there to let you specify custom titles for each post, but is that too much hassle to update regularly?

milo WordPress Hacks Series: The code in these posts was written entirely by Milo, a web designer from Oxford, Britain. Check out her website for free WordPress themes and her blog, or follow her on Twitter!

Share

  1. frekaing awesome!

  2. Wonderful article and thanks a ton for sharing the code…. This should make a lot on my blog simpler and interesting.

  3. Peter (6 comments)3 April 09

    I’m definitely too lazy to write custom post titles for every one of my posts – but I also hate having that huge block of php in my header.php file. I much prefer to wrap it in a function and put it in functions.php – then you can keep your header nice and tidy, while maintaining your functionality. Either way, definitely a good skill to remember.

  4. Bfri (1 comments)3 April 09

    For the lazy folks you can also install the Platinum SEO Plugin: :)
    http://techblissonline.com/platinum-seo-pack/

  5. For those who don’t want to go into their theme files, there’s also the All-in-One-SEO Pack (http://wordpress.org/extend/pl.....-seo-pack/), which does the exact same thing. I think it’s the more popular plugin compared to the Platinum SEO plugin, and it’s one that I use on every Wordpress website that I build.

  6. Rob (34 comments)3 April 09

    Thanks, Michael! I don’t use categories and I wanted the verbiage in the bar to be consistent from page to page, so here’s the code I’ve added, modeled after yours:

    |
    | Search Results for post_count; _e(”); echo $key; _e(‘ — ‘); echo $count . ‘ ‘; _e(‘articles’); wp_reset_query(); ?>
    | 404 Nothing Found
    | Author Archives
    |
    |
    | Archive |
    | Archive |
    |

    I’m wondering if it would shave a hair off a microsecond to replace each instance of with as you suggested in your post 13 tags to delete from your theme. What do you think?

    • Rob (34 comments)3 April 09

      Hmm … my code didn’t all show up … the perversities of trying to put code in a comment. :-/

  7. Hi Michael, amazing trick, but I have a problem and although I modified the code can not resolve, because in the test blog when the blog on the cover, the structure of titles is: Blog Name | Description | Blog Name again, repeating the name of the blog at the end, I hope to help pruning, otherwise everything works perfectly.

    Greetings

  8. jean-marc (1 comments)5 April 09

    Awesome trick!
    Just a question, why separating each line of php like this instead of writing one big block of code – I find it more readable…
    Cheers

  9. Greg (5 comments)6 April 09

    This post is broken in Opera. ;-)

    Right near the end, there’s “look for the line starting with.” I suspect that you used actual greater-than/less-than instead of &gt and <.

    Just thought you should know.

    Now to the real purpose of commenting– to let you know that this was a great trick! Having every page start with the blog name sucks. I have to admit, I haven’t implemented this just yet, but it’s on my to-do!

  10. SEO is very important part of every site which wants to gain visitors from search engines. And as one of most important seo on-page customizations are titles. Surely, this is very useful post, as in this case there is no need for plugin installation.

  11. Isn´t there something called All in one SEO pack which does quite a good job? I don’t know what it does with 404 and search results but I don’t think you want those indexed in Google at all…
    You might also wanna check that out, it’s a great plugin!

  12. tarun (1 comments)14 April 09

    thanks for the code..
    you have missed to put php in some calls to bloginfo.. ppl who doesnt know abt php wont be able to use ur code..

  13. I like using a CMS that turns the page’s heder into the title with the previous title appended and the site’s name on the very right like this:

    some specific page :: some group :: some website title

  14. Good advice with the Wordpress Title. I use the SEO Pack plug-in ( Like Bfri Commented ) that also makes the titles more search engine friendly and customisable. The SEO Pack also adds ‘No Follow No Index’ Meta tags to pages like 404 and archives etc.

  15. v.nice tweak, we use the all in one SEO plugin for wordpress. Like somebody has already mentioned it’s so easy to forget or become lazy.

  16. you don’t have to do anything manual now …. lots of SEO plugins are available now

  17. Dave (15 comments)27 April 09

    Though you can do it all through plugin but loved to learn the codes from here.

  18. Rob (34 comments)28 April 09

    I can see advantages to each method – the SEO plugin and the manual coding. The nice thing about the plugin is that it wouldn’t matter what blog theme you are using. However, with the manual coding, you can tweak things just the way you want them.

  19. futurit (1 comments)15 May 09

    Why not use special plugins? AIOS as example, this very usefull than raw code)

    futurit’s Latest Post: Helvetica World Fonts by Max Miedinger

  20. Tom (10 comments)9 June 09

    Thanks a lot for this!
    It’s of great use.

  21. Thanks, but I have a problem!

    - The blog title get’s added at the end of each title, even though it doesnt say in the code.

    Why??

    How do I get rid of it?

    Thanks again!!

    Edit: Sorry, problem solved!!

  22. Seems like there is a little error in your last line?

    I think the very last “blog title” was added to them all??

    At least that happened to me..

  23. that’s a great and easy seo tip, thanks for sharing!

  24. As I see it the main goal and the main point of not using a plugin like AIOSEO Pack is too lower the strain plugins cause to the server. However there’s still a bunch of DB queries in that code. You could just swap half of them with static info, e.x. blog name, description.

  25. Hans (17 comments)12 November 09

    I made with plugins.
    what is better? use this tutorials or plugins? I hope you can answer.

  26. Marco (2 comments)11 December 09

    Wow, very nice tutorial! thanks a lot!

  27. jurug (1 comments)18 December 09

    wowww.. nice

    I have bookmarks this tips.. thanks

Leave a Comment

Your reply will be added to the comment above (Below any other replies to this comment) -

(We DoFollow)

Not sure how to get an image with your comment?