Tech Thursday: Dynamic META Descriptions and Keywords for WordPress Tag and Category Pages

Posted on: July 28th, 2011 by Ashley Hill No Comments

  • email
  • Twitter
  • LinkedIn
  • Facebook
  • RSS

Update: The following hack did break some things on my site, so I’ve reverted to using All-in-One SEO only, and hopefully something better comes around.

 

search engine optimization

The title of this post is EXACTLY what I’m about to teach you.

Per this post about alt text for Gravatars, I use Page Grader in HubSpot, and it yells at me for things, such as no META descriptions or keywords. I’m working through my blog posts one by one to update them, but I was never able to add these fields for the auto-generated tag or category pages. Until now.

Thanks to a handy Google search, I came across this site, which has the code snippets nessesary to auto-generate META information. I did change one thing in the code, and that was to display 10 keywords per page instead of 20.

The following code should go in header.php in your WordPress theme.

META Descriptions:

<meta name=”description” content=”

<?php if (have_posts() && is_single() OR is_page()):while(have_posts()):the_post();

$out_excerpt = str_replace(array(“\r\n”, “\r”, “\n”), “”, get_the_excerpt());

echo apply_filters(‘the_excerpt_rss’, $out_excerpt);

endwhile;

elseif(is_category() OR is_tag()):

if(is_category()):

echo “Posts related to Category:

“.ucfirst(single_cat_title(“”, FALSE));

elseif(is_tag()):

echo “Posts related to Tag:

“.ucfirst(single_tag_title(“”, FALSE));

endif;

else: ?>

ADD HERE YOUR DEFAULT DESCRIPTION

<?php endif; ?>” />

META Keywords:

<meta name=”keywords” content=”

<?php

$tags = get_tags(array(‘orderby’ => ‘count’, ‘order’ => ‘DESC’));

$xt = 1;

foreach ($tags as $tag) {

if ($xt <= 10) {

echo $tag->name.”, “;

}

$xt++;

}

?>ADD, HERE, YOUR, STATIC, KEYWORDS” />

Once you add the code, your WordPress category and tag pages will now be optimized for SEO!

* Note that I use All-in-One SEO plugin, which does not currently auto-generate this information for me :(


Thanks for reading this article, if you have questions, comments or concerns, we'd love to hear them, so comment below!

And as always, make sure you're following us on Twitter and Facebook!


If you enjoyed this post, please consider subscribing to the RSS feed and following us on Twitter or Facebook.

Related Posts:


Comments are closed.