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!
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
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
Glad that recipe helped you
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
yes..it is simple
i am a chinese..
thans to your jobs
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/
Excellent, Malcom! Tanks for your enhancements on the recipe.
A pleasure!
Is this good for SEO? I mean having a page with similar meta description
<meta name=”description” content=”"/>
gives me
<meta name=”description” content=” “>
Any idea?
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!
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: