Do it Yourself: Optimize your WordPress Site Titles
The purpose of this article is to show technique and examples of optimizing your WordPress site titles by directly editing the theme header file, where the title tag is usually printed out.
By using this method you have greater control over the titles than any plugin can offer, so effectively you will be able to disable a plugin in the process, if you were using one (and one down is always good!).
Your title tag is usually found in header.php theme file. Here is the example code I use on my site.
<title>
<?php
global $post;
if ( is_single() || is_page() || is_archive() )
wp_title('');
else if(is_404())
echo '404 Error! Page Not Found';
else if(is_search())
echo 'Search for: '.wp_specialchars($s, 1);
else
bloginfo('name');
if (is_home())
{
echo ' | ';
bloginfo('description');
}
if ($post->post_parent)
echo ' - '.get_the_title($post->post_parent);
?>
</title>
Let's see what it does one by one.
Single pages and archives
if ( is_single() || is_page() || is_archive() )
wp_title('');
This code instructs that on singe post pages (is_single), pages (is_page) and archives (including categories, is_archive) default page title is written. This will be the post title or the category name. So my goal is to have only the specific phrase I have written, and nothing else.
404 Page
else if(is_404())
echo '404 Error! Page Not Found';
I have chosen simple Page not found title to let everyone know they are on a misplaced page.
Search results
If the user is on search results page, I want them to be able to quickly reference the search term by looking at the page title. This also helps search engines querying that page.
else if(is_search())
echo 'Search for: '.wp_specialchars($s, 1);
$s is the variable that holds the search term and wp_specialchars is a function for escaping printed text that you do not know in advance.
Other
In all other cases (such as home page) I simply print out my blog name.
else
bloginfo('name');
Home Page
if (is_home())
{
echo ' | ';
bloginfo('description');
}
In specific case of a home page, to the already printed blogname ('Vladimir Prelovac') I specifically add blog description to get 'Vladimir Prelovac | WordPress Expert and SEO Consultant' for my home page title.
Sub-pages
I have structured some of my pages like for example WordPress Plugins. This is the parent page for all my plugin pages.
When on a plugin page, the plugin name will be printed but I also want to print out the parent page name, in this case 'WordPress Plugins'. In this case I get for example 'Theme Test Drive - WordPress Plugins'.
if ($post->post_parent)
echo ' - '.get_the_title($post->post_parent);
To check if the page has a parent you can use check "$post->post_parent" which stores the ID of the parent page if it exists. To get the title of a post by ID use get_the_title(0 function.
Conclusion
By editing the theme file you have much better control of what the page title will be. This allows you to have much better search engine results with just a little effort, and also maybe turn off one plugin in the process. Note however that the change will remain strictly on the edited theme, and if you are the type who frequently changes themes this will not be a practical solution.
About this entry
You’re currently reading “Do it Yourself: Optimize your WordPress Site Titles,” an entry on Vladimir Prelovac site, published: Monday, November 10th, 2008
-
Check more articles like this:
- Do it Yourself: Optimize your WordPress Page Headings
- How to use custom page template in WordPress themes
- Best practice for adding JavaScript code to WordPress plugins
-
Write a comment.
-
Get my newsletter, RSS feed or twitter.
Hi! My name is Vladimir Prelovac. I am a computer engineer by profession and an adventurer by state of mind.
7 comments, newest first
Jump to main menu | Jump to comment form | Toggle comments