malcolm coles

Where to find me

Unique meta description and meta keywords in your Wordpress themes

Posted in Wordpress by Malcolm Coles on the May 26th, 2008

Update

Here is the description with both category and tag pages included:

<meta name=”description” content=”<?php if(is_home()) {echo (’’PUT YOUR HOME PAGE DESCRIPTION HERE’);} else { if(is_category()) {echo category_description();} else { if(is_tag()) {echo single_tag_title(); } else {echo get_post_meta($post->ID, “Metadescription”, true); } } } ?> “>

Original post

I know little about PHP. But after fiddling round with other people’s example code, I’ve finally managed to use custom fields to get a unique meta description and unique meta keywords on each post, and on the home page.

To get this to work, you need to add new custom fields called Metadescription and Metakeywords to each post. The ones for the home page you hard code. And I’m still working on the category pages …

Copy and paste the code below into the head section of the header bit of your wordpress template. It basically checks if it’s the home page. If it is, it uses the hard-coded meta description and meta keywords. If it’s not, it goes and looks up the custom fields and uses the values from there. So there we have it: a unique meta description and unique meta keywords in wordpress.

Unique meta descriptions in wordpress

<meta name=”description” content=”<?php if(is_home()) {echo (’PUT YOUR HOME PAGE DESCRIPTION HERE’);} else {echo get_post_meta($post->ID, “Metadescription”, true);}?>”>

Unique meta keywords in wordpress

<meta name=”keywords” content=”<?php if(is_home()) {echo (’PUT YOUR HOME PAGE KEYWORDS HERE’);} else {echo get_post_meta($post->ID, “Metakeywords”, true);}?>”>