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.
Suggested reading:
- Do it Yourself: Optimize your WordPress Page Headings
- Tweaking WordPress: Home page tweaks
- Best practice for adding JavaScript code to WordPress plugins
Posted in: SEO, WordPress
TAGS:display page name wordpress, get_the_title quotes, parent page name wordpress, post parent wordpress, wordpress api, wordpress check post parent, wordpress display page name, wordpress echo page name, wordpress echo search term, wordpress optimization, wordpress pagename name, wordpress parent page name, wordpress post parent, wordpress print is_404, wordpress seo, wordpress wp_specialchars codex






9 Comments
I was curious what would be effect of adding post names to the front page title.
like this:
"postname_1 - postname_2 - postname_3 -and more at mysite.com"
Thanks for the post. It wasn't quite what I was looking for, but your post did help me figure it out on my own.
Vladimir, it works on Home, Pages, Single pages, Archives, and Categories. But it won't work on frontpage paging. I need page title on
titlebar for the usability reason. Do you have any idea?Nice article there
helps so much!
Hi
where do i find the code editing page in my dashboard?
rds
Can't wait for the book...your site seems to have a lot of easy to understand type of articles when it comes to WordPress. Well done!
I use kind of the same thing in my blog.
I did better, I am writing a book :)
You should write an ebook about creating wordpress plugins.