« Baby got back links? | Main | Exclude Category from the main page or RSS feed in Wordpress »
Dynamic Meta Code for Wordpress
By Henrie Media Inc. | August 5, 2008
So I have wanted to have dynamically generated META Code (title, description, keywords) for my Wordpress blog for sometime now. I tried all of the plug-ins and frankly, they just didn’t get me where I wanted to go. I also didn’t want to do all the extra work of maintaining separate text for the META fields - I wanted them dynamically generated.
So I sat down and wrote the following code that I think you might find helpful to increase your blog’s SEO (Search Engine Optimization) value.
All of these changes should occur in your header.php file of your Wordpress blog’s template files.
First, I wanted to dynamically create the <title> tag with the blog’s name as the default, but include the category, archive or page title when I was browsing a category, archive or page. Here is the code to do that. Simply cut and paste this code, replacing your current <title></title> tag.
<title><?php if (is_home()): bloginfo(’name’); elseif (is_category()): bloginfo(’name’); print(” - “); single_cat_title(); elseif (is_day() || is_month() || is_year()): bloginfo(’name’); print(” - “); the_time(’F Y’); else: bloginfo(’name’); print(” - “); the_title(); endif; ?></title>
Next, i wanted to dynamically create the <META description> tag to make it unique to each page of my blog. Specifically I wanted it to display the current category or archive I was browsing or if I was browsing a page I wanted it to dynamically create the description from the first 20 words of my post. Here is the code to do that. Simply cut and paste this code, replacing the current <META description=”" /> tag.
<meta name=”description” content=”<?php if (have_posts()&& is_single()):while(have_posts()):the_post(); the_excerpt_rss(20,2); endwhile; elseif (is_category()): single_cat_title(); print(” category of the “); bloginfo(’name’); elseif (is_day() || is_month() || is_year()): the_time(’F Y’); print(” archive of the “); bloginfo(’name’); else: bloginfo(’description’); endif; ?>” />
NOTE: You can change the number of words contained in the page description by changing the first number of the_excerpt_rss() function. So in my example, I have it set to 20 words - the_excerpt_rss(20,2). If I wanted 25 words, I would change it to this - the_excerpt_rss(25,2).
Finally, the <META keywords>. I wanted to keep the keywords consistent from category to archive to page, only adding additional category names or archive attributes. Here is the code to do that. Simply cut and paste this code, replacing the current <META keywords=”" /> tag.
<meta name=”keywords” content=”<?php if (is_category()): single_cat_title(); print(”, “); elseif (is_day() || is_month() || is_year()): the_time(’F'); print(”, “); the_time(’Y'); print(”, “); endif;?>travel, blog, destination, information, see, do, help, advice, reviews, save, savings, ideas, business, golf” />
That’s all there is to it! To make it easier for you, here is all of the change in once place so you just copy and paste once! Happy SEO!
<title><?php if (is_home()): bloginfo(’name’); elseif (is_category()): bloginfo(’name’); print(” - “); single_cat_title(); elseif (is_day() || is_month() || is_year()): bloginfo(’name’); print(” - “); the_time(’F Y’); else: bloginfo(’name’); print(” - “); the_title(); endif; ?></title>
<meta name=”description” content=”<?php if (have_posts()&& is_single()):while(have_posts()):the_post(); the_excerpt_rss(20,2); endwhile; elseif (is_category()): single_cat_title(); print(” category of the “); bloginfo(’name’); elseif (is_day() || is_month() || is_year()): the_time(’F Y’); print(” archive of the “); bloginfo(’name’); else: bloginfo(’description’); endif; ?>” />
<meta name=”keywords” content=”<?php if (is_category()): single_cat_title(); print(”, “); elseif (is_day() || is_month() || is_year()): the_time(’F'); print(”, “); the_time(’Y'); print(”, “); endif;?>travel, blog, destination, information, see, do, help, advice, reviews, save, savings, ideas, business, golf” />
Print This Post
Topics: Technical Support, Web Applications |
