How to: Create a meta description function for your WordPress blog

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!

2 Responses

Dec 16 2011 11:58

I have just placed this code into my header.php and I think its working fine with the search engine. But my problem is when I share link from my blog on facebook it fetches title, image but do not excerpt and this looks really weird without any description. I tried facebook debug tool and there its fetching my excerpt. The problem is only with facebook, google+ and other social sites works fine with my site. So please help me.

Jun 05 2012 21:04

It’s clear that I should put a description into (‘Your main description goes here’), but less clear in which areas of the code I would insert additional keywords or descriptions.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox