Adding Dynamic Keywords on Meta Keyword Tag

I think it’s nearly impossible to find a person now a days, who is involved with blogging or web design related works yet never heard of the term SEO (Search Engine Optimization). Trust me, the guy who just got started with the creative web design world yesterday, he also knows what is SEO. It is not a rocket science anymore.

If you are familiar with the typical HTML meta tags then you should know by now that how most search engines (Google, Bing, Yahoo! etc.) crawls meta data from your site which helps them to rank your site. Especially if you have a blog site, where you are changing, adding contents nearly everyday, usage of these meta tags could be a very big issue.

If you take look at the source code of your WordPress site or your blog posts, chances are there that your theme is also using meta “Keywords” tag as well which might look something like the line mentioned below. Don’t be afraid, you can easily find this line on your theme’s “header.php” (generally) file.

<meta name="keywords" content="words, words, words">

If you don’t know where to find header.php file, then simply log in to your WordPress site and then go to “Appearance > Editor“. Look at the right hand side of the screen, you can’t miss it.

Now, Using a very simple snippet code you can make it dynamic which will not only help you to optimize your posts but also your site’s pages. Before I reveal the code let me explain what it does.

First of all, it will change the meta keywords on all the pages (each permalinks) of your site. This code will generate a certain number (don’t worry, you can change the number. Look at the code carefully. We are using 40 keywords here.) of top (mostly used) keywords used on your blog posts. In terms of single post, it will only display the actual keywords used on your post. Here is the code.

<?php 
if (is_single()) { 
   $posttags = get_the_tags();
   if ($posttags) {
      foreach($posttags as $tag) {
         echo $tag->name.', ';
      }
   }
} else { 
   $tags = get_tags(array('orderby' => 'count', 'order' => 'DESC')); 
   $xt = 1; 
   foreach ($tags as $tag) {
      if ($xt <= 40) { echo $tag->name.', '; }
      $xt++;
   } 
} 
?>

However, there are many ways to customize this code. You can even manually add certain keywords for individual pages if you want. But we will take about it later, on some other post. Till then stay tuned for more WordPress related snippets.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.