DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Custom WordPress Title Tags

Posted by on

By default, WordPress provides a decent way of including <title> information for your posts, pages, and various archive views. Using WordPress’ built-in template tag, “wp_title()”, we can specify several useful parameters, including:

  • sep – a string value indicating the separator displayed before the title
  • echo – a boolean value determining whether or not the title is displayed
  • seplocation – specifies the position of sep string, either left or right of the title

Here is the basic format for this tag:

<?php wp_title('sep', 'echo', 'seplocation'); ?>

..which is typically combined with the bloginfo('name') tag and used in the header.php file as follows:

<head>
<title><?php wp_title(' | ', 'echo', 'right'); ?><?php bloginfo('name'); ?>
</head>

This would produce the following output for each of the following page types:

  • The Home page – outputs the name of the site
  • Individual pages – page title | name of site
  • Single post views – post title | name of site
  • Archived post views – outputs the name of the site
  • Date-based archives – year and/or month | name of site
  • Category archives – category title | name of site
  • Author archives – public username | name of site
  • 404 error pages – outputs the name of the site
  • Search results – outputs the name of the site
  • Tag archives – tag name | name of site

For the average blog, this works fine; most pages include the title as well as the blog name, while those without specific page names simply output the name of the site instead. To go above and beyond, however, a little more preparation is needed. For example, rather than output only the blog name for search and tag pages, why not specify the exact tag or search term being displayed? And what about archive pages? We can customize those as well using the following code:

<title>
<?php
if (is_category()) {
	echo 'Category: '; wp_title(''); echo ' - ';

} elseif (function_exists('is_tag') && is_tag()) {
	single_tag_title('Tag Archive for &quot;'); echo '&quot; - ';

} elseif (is_archive()) {
	wp_title(''); echo ' Archive - ';

} elseif (is_page()) {
	echo wp_title(''); echo ' - ';

} elseif (is_search()) {
	echo 'Search for &quot;'.wp_specialchars($s).'&quot; - ';

} elseif (!(is_404()) && (is_single()) || (is_page())) {
	wp_title(''); echo ' - ';

} elseif (is_404()) {
	echo 'Not Found - ';

} bloginfo('name');
?>
</title>

When used in place of the default WordPress wp_title() tag, this code will produce clear, informative page titles for tag, search, date, author and other archive views, as well as for the dreaded 404 page. Further, this code may be modified to elaborate the various page titles however you wish, and also may be enhanced further, as explained in our book, Digging into WordPress.

3 Responses

  1. Jillian June 18, 2009

    I think there’s an error in your last block of code –

    } elseif (is_page()) {
           echo bloginfo('name'); echo wp_title('');

    I don’t think that’s supposed to be there. When the condition is true, the title ends up coming out “Blog Name TitleBlog Name”

  2. Jeff Starr

    Thanks, Jillian – I went ahead and updated the code so that the Page titles read as, “[Page Title] – [Blog Name]”. Let me know if you find anything else out of sorts. Cheers.

  3. Arpitnext June 21, 2009

    Hi!

    As we know that WP offers category-wise Feeds as well, which can be accessed by – blogurl.com/category/catogory-name/feed.

    If someone subscribe to this feed, the feed title in the RSS reader is shown as : “Blog Title >> Category-Name”.

    I want to change this title of category feed to something else (e.g. – Just only “Blog-Title”). How can I change it?

    Please help. :)

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry