Improve the page titles in WordPress

Having good and logical page titles for your blog has several benefits. The page title helps the users to see if they are on the page they intended to go to, and what kind of content they can expect to find on the page.

When Your blog is being indexed by search engines, the page title is displayed in the search result. If the page title isn’t accurate or describes what the page is about, users are less willing to click on if, which means less traffic from Google. Even worse, some search engines might ban your blog, if there are too many  duplicates in the page titles.

In this post you’ll learn to optimize the titles on your WordPress based blog, so they give users and search engines a clear understanding of the topic of the page.

Getting started

Depending on your WordPress theme and  installation, there might be different issues with your page titles. In some cases the front page hasn’t got a good and descriptive title, and in other cases, there might be problems with pages based on tags, search or categories.

Regardless of your issues, you always needs to go to the following page in the WordPress administration page to work with your page titles:

Dashboard > Appearance > Header

The header file defines the headers across all pages. In some installations/themes there’s just a universal header, that only includes the page name. Before starting to edit the file, it’s a good idea to find the place in the code between the <title></title> tags, and copy that code to a text file, for future use, if you want to restore the original settings at a later time.

Fine tuning the headers

Like in a normal html / php file, the title is defined between the <title></title> tags. WordPress has a lot of system variables the can be used to make the page titles dynamic. In this example we’ll use the following variables:

  • bloginfo(‘name’) = Name of the blog
  • single_cat_title() = Title of category
  • single_post_title() = Title of post
  • wp_specialchars($tag) = Tag, without any special characters
  • wp_specialchars($s) = Search query, without any special characters

Based on these variables, we can now optimize the titles for the most common WordPress pages. To identify on which page/template a specific title format should be used, the following variables can be used to detect which page the user is seeing:

  • is_home = The home page of the blog
  • is_category = A category page
  • is_single = A single post is displayed
  • is_page = A page (eg. “contact us”).
  • is_tag = A search result for a tag is displayed
  • is_search = A search result is displayed

For the front page, we want to display the blog name and a small catch line. Eg: “Mikes Blog :: Tips and tricks about running shoes”. Using the above mentioned system variables, we need the following piece of php, to display this title:

if (is_home () ) {
bloginfo(‘name’); echo ” :: Tips and tricks about running shoes”;
}

On the category page we want to display the category name and blog name. Eg: “Posts in the category Running Shoes :: Mikes Blog”. This requires the following php:

if ( is_category() ) {
echo “Posts in the category: “; single_cat_title(); echo ” :: “;    bloginfo(‘name’);
}

On single posts posts or single pages, we want to display the post/page name and the blog name. Eg: “Top 10 running shoes the season :: Mikes blog”.

if (is_single() || is_page() ) {
single_post_title(); echo ” :: “; bloginfo(‘name’);
}

If you have a tag cloud or tag list, a user friendly title could include the actual tag, and the blog name. Eg: “Posts tagged with Nike :: Mikes blog”.

if (is_tag() ) {
echo “Posts tagged: “; echo wp_specialchars($tag); echo ” :: “;       bloginfo(‘name’);
}

It’s also important to ensure logical title tags to search pages. Eg: “Search results: Nike running shoes :: Mikes blog”

if (is_search() ) {
echo ” Search results: “; echo wp_specialchars($s); echo ” :: “; bloginfo(‘name’);
}

And last but not least, if the page title can’t be identified as one of the above, just display the page name.

else { bloginfo(‘name’); }

To wrap everything up, you can put all the pieces together in this page title script, that really improves the page titles on your blog:

<title>
<? if (is_home () ) {
bloginfo(‘name’); echo ” :: Tips and tricks about running shoes”;
} elseif ( is_category() ) {
echo “Posts in the category: “; single_cat_title(); echo ” :: “;    bloginfo(‘name’);
} elseif (is_single() || is_page() ) {
single_post_title(); echo ” :: “; bloginfo(‘name’);
} elseif (is_tag() ) {
echo “Posts tagged: “; echo wp_specialchars($tag); echo ” :: “;       bloginfo(‘name’);
} elseif (is_search() ) {
echo ” Search results: “; echo wp_specialchars($s); echo ” :: “; bloginfo(‘name’);
} else { bloginfo(‘name’); }
?>
</title>

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

5 Replies to Improve the page titles in WordPress

  1. Jan says:

    Nice guide, however I still have a question that I hope you can answer?

    The following example explains my sitemap and how i wish to make my title look:
    – Home
    – Products > Flowers > Elder Flowers (Title: Flowers – Elder Flowers – bloginfo)
    – Procuts > Flowers > Something else (Title: Flowers – Something else – bloginfo)
    – Contact

    Is this duable?

  2. Jan says:

    I’ll just add the last two titles to Home and Contact
    – Home (Title: Custom…)
    – Products > Flowers > Elder Flowers (Title: Flowers – Elder Flowers – bloginfo)
    – Procuts > Flowers > Something else (Title: Flowers – Something else – bloginfo)
    – Contact (Contact – bloginfo)

  3. jhnidk says:

    Are products defines as categories in your blog?

    Then this code might do the trick since

    if ( is_category() ) {
    echo get_category_parents($cat, TRUE, ‘ – ‘);
    }

  4. Jan says:

    No this is the challenge…
    Products (page) > Flowers (page) > Elder Flowers (page)

    requested title:
    Flowers – Elder Fowers – My Website

    So basically I need to display the last 2 pages’ titles and lastly my bloginfo .

  5. Jan says:

    Guess there’s no “fix” to this?

Comments are now closed for this article.