How to use post tags as meta keywords and customize meta description in WordPress

By default, WordPress will not give your posts and pages any meta keywords or description.  Though meta keywords are probably obsolete, here’s a handy trick for how to use post tags as meta keywords in WordPress.

Just go to Appearance > Editor and add this code to your header.php file:

<!--BEGIN Uses post tags as meta keywords-->
<?php
if (!is_page()) {
$postTags = get_the_tags();
$tagNames = array();
foreach($postTags as $tag) {
$tagNames[] = $tag->name;
}
?>
<meta name="keywords" content="<?php echo implode($tagNames,","); ?>" />
<?php
}
?>
<!--END Uses post tags as meta keywords-->

And create a custom field called custom_description to use as the meta description, which is still displayed in search results under the page title:

<!--BEGIN Checks for custom field to determine meta description, otherwise site tagline is used-->
<meta name="description" content="<?php
$thisPost = $post -> ID;
if (get_post_meta($thisPost, custom_description, true) != "") {
echo (get_post_meta($thisPost, custom_description, true));
} else {
bloginfo('description');
}
?>" />
<!--END Checks for custom field to determine meta description, otherwise site tagline is used-->

Leave a Reply

Your email address will not be published. Required fields are marked *