WordPress: Dynamic Meta Description and Keywords howto

This is a quick guide to add dynamic meta description and keyword to each different url of our WordPress blog, very useful to SEO users.

Open your header.php file from your theme directory and put this code into:

Meta Content Description

<meta name="description" content="
<?php if (have_posts() && is_single() OR is_page()):while(have_posts()):the_post();
$out_excerpt = str_replace(array("rn", "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 Content Keywords

<meta name="keywords" content="
<?php
$tags = get_tags(array('orderby' => 'count', 'order' => 'DESC'));
$xt = 1;
foreach ($tags as $tag) {
if ($xt <= 20) {
echo $tag->name.", ";
}
$xt++;
}
?>ADD, HERE, YOUR, STATIC, KEYWORDS" />





Leave a Comment