Meta descriptions and keywords for each page and post in WordPress

April 3rd, 2009 in Web Development, Work

After searching for a plugin that might accommodate this in WordPress and coming up empty, I went digging for a possible easy way to do this with the means available.   Logic dictates that with custom fields this should be rather easy to accomplish.

The only snag might have been that in retreiving a custom field for a page or a post you need the page or post’s ID.   Usually this id is readily available within “the loop”, but what about when we’re up in the <head> tags?    A little searching in the codex reveals that we can grab your pages’ ID with $wp_query->post->ID.  Fantastic – because, with that we’re pretty much done! Adding the following in your theme’s header.php file between <head> … </head> :

<meta name=”keywords” content=”<?php $key=”meta_keywords”; echo get_post_meta($wp_query->post->ID, $key, true); ?>” />
<meta name=”description” content=”<?php $key=”meta_description”; echo get_post_meta($wp_query->post->ID, $key, true); ?>” />

.. and “meta_keywords” and “meta_description” as custom fields with your desired content for each will get you to where you want to be.

Tags: , , , ,

One Replies to “Meta descriptions and keywords for each page and post in WordPress”

  1. All-in-one-SEO Pack does this and a few other things. Here’s my question: on my “Category” pages, I’d like to get the “excerpt” to appear under each title. Any ideas?

Leave a Reply