Tutorials

How to Use Categories and Tags as Keywords

Pinterest LinkedIn Tumblr

A tag, or keyword, is a word or phrase that’s descriptive and specific to your article or web page. The right keywords will help optimize your exposure and also your positioning in search engines, which is where many people may find your article.
tagcloudAdding 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. 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().
In the Following lines of code, you will learn how to build the array of keywords and print it as the content of the <meta> tag.

Use Categories and Tags as Keywords

Here is a simple snippet for 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 } ?>

By making these changes in your theme’s header file is to help improve search engine optimization. All in One SEO Pack- WordPress  Plugin will help you to write good tags and you can easily implement them on your WordPress-powered site. You can find detail on this link: How to improve WordPress title tags.

Noor Mustafa Raza

I am a WordPress Developer and Designer, author @WPArena. I am providing Free WordPress consultation and can help you to install WordPress in a secure way to small businesses and bloggers.

1 Comment

  1. Noor Mustafa Raza
    Joe @Appsaraby Reply

    Thank you a lot for the code
    Will it work with third-party plugins like Woocommerce and products?

Write A Comment

Pin It