Post Pic

How to: Redifine title tag with a custom field

Some time ago, I shown you how to make your title tag SEO friendly. Even if it is already cool, this function can still be enhanced by using a custom field. Read on if you’re interested.

Open your header.php file for edition. find the <title> tag, and replace it by the following code:

<title>

<?php if (is_home () ) {
    bloginfo('name');
} elseif ( is_category() ) {
    single_cat_title(); echo ' - ' ; bloginfo('name');
} elseif (is_single() ) {
    $customField = get_post_custom_values("title");
    if (isset($customField[0])) {
        echo $customField[0];
    } else {
        single_post_title();
    }
} elseif (is_page() ) {
    bloginfo('name'); echo ': '; single_post_title();
} else {
    wp_title('',true);
} ?>
</title>

Then, if you want to define a custom title tag, simply create a custom field named title, and give your title as a value.

14 Responses

Jan 30 2009 20:27

Hi, this is exactly what I’ve been looking for — thanks very much! Just one question — as of now, on a single post, only the title of the post is generated and displayed. Is there any way for me to also display the title of my blog after the title of the post? Thanks!

Jan 31 2009 04:03

Never mind, I figured out how to do it myself. Thanks again and keep up the great work.

Feb 06 2009 03:11

I usually just code my title tag so this is unnecessary, but it’s always good to know. Keeps expanding my knowledge of Wordpress ;)

May 15 2009 12:52

Ups, it looks like I cannot paste the code :(

Nevertheless, i just copied / paste the code you provided.

Jun 03 2009 10:14

very cool , all of your Tutorials just awesome :)

Jul 09 2009 13:23

A simpler option is to use the All in One SEO Pack plugin which lets you customise the title and description meta tag for each post. http://su.pr/1K5SCu

Oct 02 2009 15:53

This trick was very useful for me. Thanks!!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required