WordPress SEO: Add meta description to your theme

I always asked myself why most WordPress theme don’t display any meta description. I created a short recipe to display a meta description, so read on and don’t hesitate to use it on your own theme!

Open your header.php file. Paste the following code anywhere within the <head> and </head> tags:

<?php if (  (is_home()) || (is_front_page())  ) { ?>
<meta name="description" content="Blog description goes here" />
<?php } elseif (is_single()) { ?>
<meta name="description" content="<?php the_excerpt();?>"/>
<?php } ?>

Good job! Your theme now have meta description on the homepage as well as single posts. Also, we should create another conditionnal structure to handle categories.

13 Responses

Oct 27 2008 00:16

You read my mind - I’ve just been wondering how to do this.

Great idea for a site, btw, I can see your recipes coming in very handy

Oct 27 2008 00:18

Glad that recipe helped you :)

Oct 29 2008 11:46

Really simple and clean. Bravo.
When I use the plugin AllinoneSEO, I think I don’t need this but I find it a good and clean solution.
Happy to find your blog thanks to the french http://www.lejournaldublog.com/
See you soon
Thierry

Nov 01 2008 17:01

yes..it is simple
i am a chinese..
thans to your jobs

Nov 20 2008 00:17

If you use some custom fields, you can make this even more sophisticated: http://www.malcolmcoles.co.uk/blog/unique-meta-description-and-meta-keywords-in-your-wordpress-themes/

Nov 20 2008 00:20

Excellent, Malcom! Tanks for your enhancements on the recipe.

Nov 20 2008 01:03

A pleasure!

Dec 05 2008 14:06

Is this good for SEO? I mean having a page with similar meta description

Dec 10 2008 11:54

<meta name=”description” content=”"/>

gives me

<meta name=”description” content=” “>

Any idea?

Jan 30 2009 20:22

Hi, thanks for the post — great code. I’ve been using it and it works great, but I noticed that when it uses my excerpt for the meta description, it’s generated with and tags surrounding it. Is there any way to get rid of these tags? Thanks!

Jan 30 2009 21:20

I don’t think the s matter - google doesn’t show them. But there’s a strip_tags command in PHP that will remove them if you play around with it … Try this page to see how it works: http://wordpress.org/support/topic/198541

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required
Blog And Make Cash