Custom Wordpress Meta Titles - Homepage, Pages, Posts

22 January, 2012 by Tom Elliott

The Meta Title still remains one of the most important content-based SEO factors when determining a web pages ranking in search engines. If you’re like me, and slightly obsessive with your WordPress SEO or like finer control over your WordPress Meta Titles, then the below code snippet can help create custom Meta Titles for the common WordPress page types – including the homepage, pages, individual posts or category archive pages.

These common page types are identified by the below built-in WordPress functions:

First of all, you’ll want to locate the main “<title></title>” tag. In most themes, there should be a header.php file which will contain all the Meta tags, document declaration, css files and scripts etc. Typical code for existing title tag may look something like:

<?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?>.

The wp_title() changes across page types and usually includes the title of your blog page, or title of your post with bloginfo(‘name’) displaying the the blog’s name at the end of the title. In some cases, you may not wish to display the name of your blog on every page, or you may want to have custom page titles for your pages or categories.

By replacing the above line of PHP code with the below chunk, then you can start to give yourself greater custom control over your WordPress Meta Titles:


<?php echo $blog_title; ?>

With the above code for example, I display the post title only for each of my posts, the homepage has the blog name followed by the blog description and the category archive pages have the category name followed by ‘archive’ and the blog name. You could go even further and name specific pages – the below PHP code will do just that:

if (is_page('page-name-url')) {
 $blog_title = "custom META title";
}

There are also other page types, such as 404 or Tag archive pages, so checkout the full WordPress function list if you need additional page types.

There will be a number of plugins around that will achieve the same or similar outcome but in many cases I prefer getting into the nuts and bolts of the PHP which means I know exactly how my new Meta Titles will look.

Note: It’s worth making a backup of the original code or header.php file. If you already have plug-ins installed to control the Meta Title or other META elements, changing the code for the title may adversely affect the installed plugin.



9 Comments

  • PTCL IVIO Icon Pro says:

    Thank you for sharing. I have been messing with wordperss titles and meta tags for a while with a little succes but you made my life easy dude. Hats off to you.

  • Yinyin says:

    A great help !
    How can I add the category name to a post title ?
    A unique category is attached to all my posts.
    Regards.

  • Tom Elliott says:

    Thanks Yinyin,

    If you want your category name on post pages, you could try the following:

    if (is_single()) {
    $blog_title = get_the_title() . " - " . single_cat_title("",false);
    }

  • Thanks a lot, Tom – I found this very helpful, much appreciated!

    I modified the index.php call and added a call for Tags as well.

    This allows for the index.php page (posts, not static) to display a custom title with the site/blog name and will also add the Tag page title and site/blog name to Tag pages.

  • Sorry, forgot to remove the PHP call…here’s the syntax.

    $blog_title = get_bloginfo(‘description’) . ” Index Page Title | ” . get_bloginfo(‘name’);
    if (is_home()) {
    $blog_title = get_bloginfo(‘name’) . ” | ” . get_bloginfo(‘description’);
    }
    if (is_single()) {
    $blog_title = get_the_title() . ” | ” . get_bloginfo(‘name’);
    }
    if (is_tag()) {
    $blog_title = single_tag_title(“”,false) . ” | ” . get_bloginfo(‘name’);
    }

    if (is_category()) {
    $blog_title = single_cat_title(“”,false) . ” | ” . get_bloginfo(‘name’);
    }
    if (is_page()) {
    $blog_title = the_title();
    }

  • Yuan says:

    Phuuuh, finally find out the solution, thank you so much! Didn’t work out of the box though since it gave me a weird “/hostname/pagename” for page titles. So this got me what I wanted:

    home page: Site name | Description
    individual pages: Page name | Site Name

    cheers!

  • Yuan says:

    & sorry number 2! 🙂 Forgot to remove the php…

    $blog_title = get_bloginfo(‘name’);
    if (is_home()) {
    $blog_title = get_bloginfo(‘name’) . ” | ” . get_bloginfo(‘description’);
    }
    if (is_page()) {
    $blog_title = . get_the_title(); . ” | ” . get_bloginfo(‘name’)
    }

  • Aniket says:

    I would like to remove “- Page 2” the pagination link only from the page title by not affecting the actual pagination. How do i proceed with this?

  • James says:

    How to add meta tile and description on Category pages.. My SEO plugin isn’t showing Meta Box..

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    css.php