WordPress meta description using post excerpt, category description and post ID

Last updated on November 28, 2011. Tags: , ,

Even though the major search engines already dropped meta description in calculating the importance of a web page for a particular search query. it is still used to provide snippets when your web page appears to its search results.

The meta description of a web page can be found in the title tag between the tags <head> and </head>. It is coded as:

<meta name="description" content="Provide a short description of your web page or website here." />

Just like in case of page title and meta robots, HTML elements that are also between the header tags, the meta description will be easier to assign uniquely for each page if the website is made-up of static HTML web pages. However, in case of WordPress-powered websites, the only place where you can put meta description is between <head> and </head> in the header.php of the WordPress theme that you are using.

This tweak was tested on WordPress 2.6.1 and 2.9.1. It's highly likely that it will work on versions between these two and will probably work on other close versions.

Using the post excerpts as meta description

Unlike HTML title tags, most WordPress themes do not have the means to provide each page and post with unique meta description. However, in case of posts, we can use post excerpt; the summary that you can provide in a smaller text area below the post content text area.

To use the excerpt as meta description:

  1. Calling the post excerpt, by default, automatically insert paragraph tags. Meta description (and any element in the header) do not use paragraph tags. You need to disable the automatic insertion of paragraph tags as explained in this post.
  2. Open the header.php and insert the following PHP code somewhere between <head> and </head> (assuming that there is no meta description in the header.php. which is the case for most WordPress themes; if there's a meta description, just delete it and paste the code below).
<meta name="description" content="
<?php if ( is_single() ) { the_excerpt(); }
else { echo'Insert generic website description here'; }
?>"
/>

So how does this code work? WordPress extracts the excerpt of the post and assign it as the meta description. For example, if you have a post entitled "Christmas Bacons" and you typed in the excerpt text area "Bacons really tastes delicious" before publishing or saving it, it's meta description will be like:

<meta name="description" content="Bacons really tastes delicious" />

Note that the code above works only in posts. If the dynamic web page is a page, a search result or a category, the meta description will be whatever you typed in place of "Insert generic website description here". If the excerpt of a post is blank, you will also get a blank meta description; something like meta name="description" content="".

Using the category description as meta description

This meta description will appear only if the dynamic page is a category page (the page where all posts under that category are listed). Using category description as meta description is similar to post excerpt in the following ways:

In the code below, the third line calls the category description. It is similar to the PHP code above except that when a category page is visited, the meta description will be the category description. All dynamic pages except for the posts and category pages will have the "Insert generic website description here" as meta description.

<meta name="description" content="
<?php if ( is_single() ) { the_excerpt(); }
elseif ( is_category() ) { echo category_description(); }
else { echo'Insert generic website description here'; }
?>"
/>

Using the post ID to assign meta description

Another way of assigning unique meta descriptions is to use the post ID to provide separate if argument for each page. This can be more confusing because the short summary of a post is typed in a separate location (i.e., the header.php) instead of being typed adjacent to the actual post. However, it is useful for pages because:

  1. pages are usually much fewer than posts and thus gathering their meta descriptions in one file is much less confusing, and
  2. it is not possible to write excerpts for pages (the separate text area for excerpt is not available)

Let's say we have a typical WordPress blog with several posts and only three pages: "Home", "About Us", and "Contact Us", with post IDs of 3, 9 and 27 respectively. Then PHP code for the meta description can be as follows"

<meta name="description" content="
<?php if ( is_single() ) { the_excerpt(); }
elseif ( is_category() ) { echo category_description(); }
elseif ( is_page(3) ) { echo'Describe the Homepage'; }
elseif ( is_page(9) ) { echo'Describe the About Us Page'; }
elseif ( is_page(27) ) { echo'Describe the  Contact Us Page'; }
else { echo'Insert generic website description here'; }
?>" />

In the code above, each line with post ID assigns a meta description for the page with that post ID, the posts obtain their meta descriptions from the excerpt, the category pages get their meta description from the category description, and the rest of the dynamic pages use the 'generic website description'.

NOTE: If you are using the default home page (list of recent posts), simply replace is_page(3) with is_home() in the code above for it to work.

Most websites are accessed through the search engines, and most blogs are accessed by having one of its posts, usually not its home page appear in the search results. Meta description provides a short teaser to entice the visitors browsing through the search results to visit your website. Therefore, it is essential that each of these posts and pages to have its own meta description.

Posted by Greten on January 25, 2009 under Hypertext Mark-up Language, WordPress

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by JessicaMem on 05.10.09 6:01 pm

    I really liked this post. Can I copy it to my site? Thank you in advance.

  2. Posted by Greten on 05.11.09 3:58 pm

    Hi Jessica. You cannot copy it. Both of us will suffer duplicate content and will cause our websites to be pulled down in search results.

    I suggest you study this article, experiment on the codes, and then write your own article based on the results of your experiments.

  3. Posted by Andreas on 10.25.09 12:29 pm

    Thanks very much for this I was looking to update my wordpress to show the excerpt as the description for google to see and this was the perfect solution.

  4. Posted by Andrew on 12.11.10 6:33 pm

    Thanks for the tip. I can add this – WordPress has tag_description function using which you can use your tag descriptions inside the meta tag. So… You can add another elseif with is_tag();… I’ll try to post an example, dunno if it will be filtered out:

    elseif ( is_tag() ) { echo strip_tags( tag_description() ); }

    strip_tags is needed because by default WordPress adds p tags around tag_description.

  5. Posted by Che on 12.11.10 8:41 pm

    I’m not sure how did you get it working. It clearly said here
    http://codex.wordpress.org/Function_Reference/the_excerpt

    that the_excerpt function only works within the lopp.

  6. Posted by Greten on 12.16.10 1:45 pm

    @Andreas – your welcome!

    @Che – As far as I know, WordPress codex operates like a wiki. Most of the information provided there are written by WordPress users who took some time to volunteer writing them. Perhaps, it just happened that none of the volunteer writers encountered this tweak.

    @Andrew – Thanks for the information. I will test it sometime later although I don’t use tags that much.

  7. Posted by Carlo on 03.05.11 4:24 pm

    Hi Vicke.
    great post.
    I implement the code for insert category description only addidng the meta tag:
    <meta name="description" content="”/>

    but now i have not description on post ( and i believe in page too).

    So could you suggest me the code to add in the code above to permit that my headspace2 description do its work:-))).

    Regards

  8. Posted by Greten on 04.03.11 7:36 am

    Hi Carlo,

    I’m not quite sure what you are trying to do here. From what I understand, you use a slightly different code from what I described here and you are trying to use the category description as meta description of static pages and posts.

    If how I understand what you are saying is correct, then that’s not possible. Category description works only on category pages. It does not work in static pages and in posts, even if that post belongs to specific category.

  9. Posted by Matthew Slyman on 07.04.11 1:18 pm

    Truly excellent article: 10/10. WordPress Codex was sending me around in circles—this article gave me precisely what I wanted, straight away.

    One small improvement (for WordPress 3.1.4 with theme derived from TwentyTen): my theme uses HTML tags in the_excerpt(), e.g. for the “Continue reading →” hyperlink.

    I find the following solution more satisfactory and complete:

    if (is_single()) { echo(strip_tags(get_the_excerpt())); }
    elseif (is_category()) { echo category_description(); }
    elseif (is_home()) { bloginfo(‘name’);bloginfo(‘description’);}
    else { bloginfo(‘name’);echo(‘&#x2014′);wp_title(‘|’,true);}

    Note the PHP function, “strip_tags(get_the_excerpt())”

  10. Posted by Greten on 07.07.11 10:48 am

    Hi Matthew

    Excellent solution you got there. Hopefully, I will be able to try it as soon as possible (no time to test codes lately being busy to my other blogs and my full time work).

    If the strip_tags is what I think it is, I think it should also be placed in the category description line since category description also puts paragraph tag by default.

  11. Posted by Tyldor prestiti personali on 07.10.11 10:11 pm

    Hi, do you know if there is a way to generate meta tag description that contains variabile? ( example for category page)

    Something like “this is the meta description for this tag: $tag”

    Ps: sorry for my english but it’s not my mother language.

  12. Posted by Greten on 08.17.11 10:17 am

    Hi Tyldor, sorry I didn’t know of any way to do that and I could not imagine of any reason to use variables in meta tags. Good day!

Post Comments





Comment Rules and Reminders

  • The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
  • Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.