Last month, I wrote a recipe about how to add meta description to your theme. Malcolm Coles, a WpRecipes reader, made some great enhancement to my original function. Here’s to create a meta description fnction.
Last month, I wrote a recipe about how to add meta description to your theme. Malcolm Coles, a WpRecipes reader, made some great enhancement to my original function. Here’s to create a meta description fnction.
Open your header.php file. Paste the following code anywhere within the <head> and </head> tags:
<meta name="description" content="
<?php if ( (is_home()) || (is_front_page()) ) {
echo ('Your main description goes here');
} elseif(is_category()) {
echo category_description();
} elseif(is_tag()) {
echo '-tag archive page for this blog' . single_tag_title();
} elseif(is_month()) {
echo 'archive page for this blog' . the_time('F, Y');
} else {
echo get_post_meta($post->ID, "Metadescription", true);
}?>">
Credits goes to Malcolm Coles for this awesome recipe!
21 Responses
Isn’t it way better to simply use HeadSpace2 or All-in-one-SEO-pack plugins?
They tend to do this with a lot more on top of it.
@Tsahi: It depends for who. Some people prefer using plugins, some others prefers using hacks. Personally, I think that even if you’re using a plugin, knowing how it works is always a good thing, especially if you’re a theme designer/developer, so you can implement top functionallities on your theme
jbj – I’ll vote for knowing how stuff work every day
Thanks for the thanks! Thought I’d better point out that the ($post->ID, “Metadescription”, true) bit relies on you adding a custom field called “Metadescription”. It’s very easy to do (the custom field box is towards the bottom of the standard Wordpress write-post page) but you do need to do it or it won’t work.
An alternative is to use the existing excerpt field, and change the code as per your original post last month.
The advantage of the custom field approach is that you can make it different to the excerpt (which I was already using on my category pages, like this one: http://www.malcolmcoles.co.uk/blog/category/wordpress/).
But although this is more flexible, I have to confess that, lately, I’ve become a bit lazy and have been copying the excerpt into the metadescription custom field, so maybe the excerpt is the way to go!!
You’re welcome, Malcolm! You did a great job by enhancing the function I had previously provided. That’s the power of open-source
PS This method, along with plugin ways to generate meta descriptions (I think All in One SEO suffers from the same problem), doesn’t generate unique meta descriptions on page 2 or higher of a loop.
So if a given category runs to three pages, for instance, then all three pages will have the same meta description using the method described here.
I think All in One SEO leaves the meta description off pages 2 or higher. But that doesn’t really solve the problem either!
I’m sure there must be a way for wordpress to know which page of the loop a given page is, and then we could add something to the PHP to include ‘- page X’ at the end of the meta descrption when X is greater than 1.
Anyone know how (hey, you said you wanted to know how stuff works!)?
@malcolm – look at page navigation plugins to Wordpress. To add the page number along with how many pages there are, they go to the globals wpdb and wp_query and calculate it. It’s ugly but working.
I hacked my way into some of them to add page navigation on a blog once.
I use headspace but well, since there are some themes that “may” have a problem on that plugin, this should be useful
Thanks. I prefer not to use plugins as far as possible, so this is very helpful.
Page problem solved! Tsahi – I had a good hunt round some plugins and found a variable called $paged. After that, it became clear that you didn’t really need a plug in. Anyway, click my name to see how I did it.
Thank you super, this is a great post… You give me lot more idea now =)
I’m using Headspace, but all subpages have the same metadescription (homepage,category and tag). Is any way i can solve this while keeping headspace? thanks in advance!
Thanks. I used to use excerpt RSS for meta descriptions. Like this:
<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<meta name="description" content="<?php the_excerpt_rss(); ?>" />
<?php endwhile; endif; elseif(is_home()) : ?>
<meta name="description" content="<?php bloginfo(‘description’); ?>" />
<?php endif; ?>
Hi. I tried putting the code just before the tag and it still doesn’t work. should I do anything else?
Trackbacks: