The Perfect Title Tag

In Version 2.5 Template Tag wp_title got an update, therefore Plugins became useless for that.

By the way, the code came from the Plugin „Optimal Title“.

The title tag is in the header, therefore in header.php . The following syntax works in all WP-versions, but it's not a perfect way for WordPress higher than 2.5. I will show you a perfect title tag for WordPress 2.6 and higher.

<?php
if ( is_archive() ) { _e('category'); } wp_title('');
if ( is_archive() ) { _e(' - '); }
if ( is_single() ) { _e(' - '); }
if ( is_page() ) { _e(' - '); } bloginfo('name');
?>

Since version 2.5 the function wp_title has following parameters, also they are already filled and it's not really necessary to use parameters.

  • sep - delivering a separator character, separator or text before and after the post
  • display - displaying title; interesting if you don't want to display the title, instead a variable and use it furthermore
    • 1 (True) - standard
    • 0 (False)
  • seplocation - left (left) or right (right) of the post title should be the character or text. Below are 2 examples how it could look like.
wp_title($sep = '»', $display = true, $seplocation = '')

The above variant for WordPress earlier than version 2.5 could look like this in WordPress Version 2.5*.

<?php if ( is_archive() ) { _e('category'); } wp_title('-', true, 'right'); bloginfo('name'); ?>

Display with different parameters seplocation looks like that. The blog name is „WordPress Book“.

Left

<title> - WordPress Plugins for better security - WordPress Book</title>

Right

<title>WordPress Plugins for better security - WordPress Book</title>

I like to display the category, since it's an added value for the reader, especially if they like to bookmark the page. It isn't relevant for search engines since I limited indexing it by following:

<?php if((!is_paged() ) && ( is_single() || is_page() || is_home() || is_category())){ echo '<meta name="robots" content="index, follow" />' . "\n"; } else { echo '<meta name="robots" content="noindex, follow, noodp, noydir" />' . "\n";} ?>

<meta name="description" content="<?php if ( is_single() ) { wp_title(''); echo ' - '; } elseif ( is_page() ) { wp_title(''); echo ' - '; } bloginfo('description'); ?>" />

In this combination you can replace !is_paged() ) && ( is_single() with the new tag is_front_page() on your front page. This conditional tag is available since version 2.5.

Further resources

Comments are closed.

4 comments

  1. Ryan McCue

    _e() is not an alias for echo and should not be used as such. It is for translation purposes.

    All of your _e(' - '); lines should be echo ' - ';

  2. Michael

    Yes i know, but we are german and we use those functions in our themes for translation purposes. Sorry for the mistake ;)

  3. jbj

    Very nice post, lots of usefull information. Thanks for sharing :)

  4. Rob Mason

    The seplocation thing had been bugging me for ages and couldn't find a suitable solution. Now I have! Huge thanks

    R