Using Categories and Tags as Keywords

Long Tail Pro Keyword Research

Keyword Research for SEO

Get Instant Access

If you need to add meta keywords to your single post archive template, you have all the data you need: your categories and tags. All you have to do is get a combined list, separated by commas.

In this example, you first use a conditional tag to make sure this code is used only on single post archives. (Pages don't have categories or tags, and the get_the_tags() and get_the_category() functions won't work correctly on archive pages containing multiple posts.) Then, for each tag and category, you force the name to all lowercase and add it to an array of keywords. As you print the <meta> tag, you remove duplicates from the array with array_unique() and convert the array to a comma-separated string using implode().

Listing 7-16 shows how to build the array of keywords and print it as the content of the <meta> tag.

Listing 7-16. Creating meta keywords tag from post categories and tags in header.php

<?php if (is_single()) { foreach((get_the_tags()) as $tag) { $keywords[] = strtolower($tag->name);

foreach((get_the_category()) as $category) { $keywords[] = strtolower($category->cat_name);

<meta name="keywords" content="<?php echo implode(", ", array_unique($keywords)); ?>" /> <?php } ?>

Was this article helpful?

0 0
The Keyword Research Bible

The Keyword Research Bible

Sick And Tired Of Getting Tons Of Traffic That Isn't Buying? Sounds Like It's Time To Reexamine Your Keywords. Do you know how important keywords are to your business?

Get My Free Ebook

Post a comment