The Meta Title still remains one of the most important content-based SEO factors when determining a web pages ranking in search engines. If you’re like me, and slightly obsessive with your WordPress SEO or like finer control over your WordPress Meta Titles, then the below code snippet can help create custom Meta Titles for the common WordPress page types – including the homepage, pages, individual posts or category archive pages.
These common page types are identified by the below built-in WordPress functions:
- is_home() will identify the homepage
- is_single() will recognise an individual blog post page
- is_category() will identify a category archive section
- is_page() will identify if a page is visited.
First of all, you’ll want to locate the main “<title></title>” tag. In most themes, there should be a header.php file which will contain all the Meta tags, document declaration, css files and scripts etc. Typical code for existing title tag may look something like:
<title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title>.
The wp_title() changes across page types and usually includes the title of your blog page, or title of your post with bloginfo(‘name’) displaying the the blog’s name at the end of the title. In some cases, you may not wish to display the name of your blog on every page, or you may want to have custom page titles for your pages or categories.
By replacing the above line of PHP code with the below chunk, then you can start to give yourself greater custom control over your WordPress Meta Titles:
<?php $blog_title = get_bloginfo('name'); if (is_home()) { $blog_title = get_bloginfo('name') . " | " . get_bloginfo('description'); } if (is_single()) { $blog_title = get_the_title(); } if (is_category()) { $blog_title = single_cat_title("",false) . " archive | " . get_bloginfo('name'); } if (is_page()) { $blog_title = the_title(); } ?> <title><?php echo $blog_title; ?></title>
With the above code for example, I display the post title only for each of my posts, the homepage has the blog name followed by the blog description and the category archive pages have the category name followed by ‘archive’ and the blog name. You could go even further and name specific pages – the below PHP code will do just that:
if (is_page('page-name-url')) { $blog_title = "custom META title"; }
There are also other page types, such as 404 or Tag archive pages, so checkout the full WordPress function list if you need additional page types.
There will be a number of plugins around that will achieve the same or similar outcome but in many cases I prefer getting into the nuts and bolts of the PHP which means I know exactly how my new Meta Titles will look.
Note: It’s worth making a backup of the original code or header.php file. If you already have plug-ins installed to control the Meta Title or other META elements, changing the code for the title may adversely affect the installed plugin.
Thank you for sharing. I have been messing with wordperss titles and meta tags for a while with a little succes but you made my life easy dude. Hats off to you.