Dynamic META Description For Your Wordpress Blog
If you examine Google's search result page, you'll notice that the description (the text under the linked title) displays the page's META description if present. Therefore, putting a different META description in each of your website's pages is important if you want to attract clicks in search result pages where your site ranks. If your description complements the title well, you can attract more clicks. This blog uses this technique so if you go to any of my pages here, you'll notice I have a different META description for each page.
What I'm going to show you in this post is how you can place a dynamic META description in each page of your Wordpress blog. I'm not sure if there are any Wordpress plugins out there that can do this but I find this method more efficient than installing a plugin because this only involves modifying your theme. If you're a Wordpress theme designer, I suggest you do this in all of the themes that you make.
It's pretty simple. You edit your theme's header.php and add a META description tag in the head section that has a dynamic value. You can make its value dynamic by using PHP and Wordpress template tags. This is how it works.
First, you create a conditional that checks if the page the user is viewing is the homepage, or a single post or page, or a category page.
If it is the homepage, put the blog description as the value of the META description.
If it is a single post or page, put the value of a custom field you created with "description" as its key.
If it is the category page, put the category's description as the value of the META tag.
And that's it. Now that we understand the concept, let's put it into implementation. The code sample below shows the conditional statement inside the META description's value.
-
<meta name="description" content="
-
<?php
-
if (is_single() || is_page()) {
-
# Single post/page.
-
} elseif (is_category()) {
-
# Category page.
-
} else {
-
# Default meta description (homepage)
-
}
-
?>
-
" />
This is just the conditional without any action yet. The first "if" statement checks if the current page being viewed is a single post or page. The "elseif" fallback condition checks if it is a category page. And finally, the default action to be executed (the default value of the META tag) will be within the "else" block. Now let's put the corresponding action for each condition. (I wouldn't be showing the meta tag here so the syntax highlighting works unlike the sample code above.)
-
if (is_single() || is_page()) {
-
# Single post / page.
-
# Use the 'description' custom field.
-
} elseif (is_category()) {
-
# Category page.
-
# Use category's description
-
} else {
-
# Default meta description
-
# Blog's description
-
}
For the first action within the "if" condition, it "echoes" the value returned by get_post_meta (a Wordpress template tag). This function returns the value of a custom field. The parameters of get_post_meta are the post ID, the key of the custom field, and the "single" parameter (determines whether you want to return the first value of the field), respectively. The property of the Wordpress variable $post, ID ($post->ID), gives us the post ID of the current single post being viewed.
The second action inside the "elseif" block echoes the category description. The Wordpress template tag category_description is used. Usually, this function has tags and new line characters in its value so I filtered it with "trim" and "striptags". The default action inside "else" echoes the blog description using Wordpress' bloginfo funciton.
Like I said, there could be some plugin out there that does the same thing so if you know one, please tell us by commenting to this post. But I like this "hack" because of simplicity and efficiency.
Posted on: May 17, 2008 Filed under: Wordpress
Comments (2)
Mike
August 14th, 2008 at 5:00 am
Hello,
Could you please explain how to modify the code below to use posts excerpt instead of a post’s title?
<meta name=”description” content=”" />
Thanks,
Mike
Mike
August 14th, 2008 at 5:03 am
Oops, it seems the code I pasted did not go through.
Basically, I am trying to figure out the code which would only output a page excerpt in the description meta tag.
Thanks,
Mike
Leave a reply