Thursday, February 3, 2011

Wordpress page title tag modification for dynamic titles

This code will get the web page title and description to automatically change from a page to a category to a post. That way the blue status bar at the top of your browser will more accurately reflect what you are seeing. Also If you roll over the browsers tab the box that appears will reflect what page you are on.

Basically what is says is is this a single post? If so, show the post title. Is it a category? If so, show the category name. Otherwise show the blog description.

< title > 
< ?php if ( is_single() ) 
{ bloginfo('name'); echo " - "; 
single_post_title('', true); }
elseif ( is_category() ) { bloginfo('name'); 
echo " - "; $category = get_the_category();
echo $category[0]->cat_name; }
else { bloginfo('name'); echo " - "; 
bloginfo('description'); }
? > < /title >

< meta name="description" content="
< ?php if ( is_single() ) { 
bloginfo('name'); echo " - "; 
single_post_title('', true); }
elseif ( is_category() ) { 
bloginfo('name'); echo " - "; 
$category = get_the_category();
echo $category[0]->cat_name; }
else { bloginfo('name'); echo " - "; 
bloginfo('description'); }
? > / >

No comments: