solostream

Hard-coding an SEO friendly title in the WordPress header file

| November 12, 2007 | 5 Comments

The title I am referring to here is the title that appears in the top bar of your browser, and is in betweeen <title></title> tags in your code. Apparently, to make it more SEO friendly, the title of the post or article should appear before the title of your blog, but generally themes have it programmed the other way around.

You can switch it through the use of plugins, but if you want to avoid using another plugin you can fix this in the header.php file. Find the code that starts with <title>, and replace what is currently there with this:

<title><?php if ( is_single() ) { ?> <?php wp_title(''); ?> &raquo; <?php } ?><?php bloginfo('name'); ?></title>

This tells WordPress that if the page is not single, the name of the blog will appear only. This is for the homepage. If the page is single, i.e. a single post page, then the name of the post will appear and then the blog title. So, for example, a post on WordPressGarage would have the following title:

Yet another way to feed your facebook obsession ยป WordPressGarage

Tags: ,

Category: Code Snippets

Elegant WP Themes

Comments (5)

Trackback URL | Comments RSS Feed

  1. Anna Vester says:

    Wow, that’s pretty powerful. Thanks for this!

  2. Ryan says:

    I prefer my pages to show up in the same way as you have your single posts setup, so I use a different method. The text in this is probably going to goof, but I’ll try anyway …
    <title><?php wp_title(' '); if(wp_title(' ', false)) { echo ' | '; } echo 'New Zealand Hockey | '; bloginfo('name'); ?></title>

    My site at http://flytrapgrowing.info/ uses this method if you want to see it in action.

  3. Ryan says:

    One more thing. Why the heck do theme designers do their titles the way they do? Nearly everyone agrees that the way you are suggesting here is better, but yet theme designers still continue to use the same system as the default Kubrick theme. It seems nonsensical to me. Or maybe I’m missing something?

  4. [...] Recent Posts in WordPress sidebar and Hard-coding an SEO friendly title in the WordPress header file – Miriam posted some very simple and powerful [...]

  5. [...] simple solution is to reverse the code as explained at the WordPress garage (and quoted below for convenience): You can switch it through the use of plugins, but if you [...]

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.