WordPress : How to display only the most popular tags in a tag cloud

wordpress-showing-most-popular-tags-in-tag-cloud

In my previous article about WordPress tag cloud widget, I showed you how you can display a count number for each tag. But what if you want to limit tags that are shown by displaying only the most popular tags and hide those with low count values. The tag cloud in WordPress doesn’t have the parameter for that behavior, but by using another parameter, we can achieve the same result. In this article, we will add the code that will show only those tags in the tag cloud, that have count parameter value of 3 or more.

We can set many parameters for our tag cloud widget using wp_tag_cloud function. Among the list of parameters is a number parameter that can be used to set a fixed number of actual tags to display, but this is not what we want. We want to limit number of tags based on count parameter.

There is no such parameter in wp_tag_cloud, but we can use exclude parameter to produce the same behavior. All we need to do is get the list of term_id tags, that we want to hide.

Warning: If you use include parameter when calling wp_tag_cloud, the exclude parameter will be ignored, so the solution in this article will not work.

Getting the list of term_id tags with low count number

To get this list, we will create a function that accepts a number as a parameter. This number will represent the count upper limit that we want to hide. If the tag has a count number lower or equal than that of parameter, the code adds it to the excluded list. The function will return a string of comma separated list containing term_id post tag values. We will use this result later on.

The code for this custom function is below:

Code explanation

  • Line 3:

    First, we call get_tags function, that returns array of objects containing post tags. By setting the orderby parameter to count, we will get sorted list of post tags in ascending order starting from those with lowest count value.

  • Lines 5-10:

    Here we loop through the array of objects we got from get_tags function. Since that array of tags is sorted by count from lowest to highest, we don’t need to loop through all of the array. We can break from the loop as soon as the loop reaches the tag with the count number bigger than the maximum number we want to exclude (lines 7 – 8), but until that happens, we add in each iteration a term_id of the current tag object into the excluding array (line 9).

Before the function returns the result, it joins the array of excluded elements to the string containing a comma separated list. Now all that is left is to use that list as a value to exclude parameter for our tag cloud.

MyThemeShop - Premium WordPress Themes & Plugins
Choose between Blog, Magazine, WooCommerce, Business Themes.
  • Fully Responsive Designs
  • Premium Support 24x7
  • 30 Day Money Back Policy
  • Buy Once, Use for Lifetime
  • SEO Friendly
  • Use On Unlimited Sites
Check it now
This is an affiliate ad.

How to use the result of previously created function to display a Tag Cloud

To display a tag cloud in a theme template, we use wp_tag_cloud function. It accepts different parameters, one of which is the exclude parameter and we use that parameter to limit tags in the tag cloud. Here is how the code would look like:

First, we call our get_tags_termid_to_exclude() function from previous section and use ‘2’ as an argument, meaning it will return a list of term_id tags, that have count number 2 or lower. This way, we limit tags to only those with count number 3 or higher. Next we create the array that will contain custom parameters for the tag cloud. One of them is ‘exclude’ parameter that we set value to $excluded_tags variable containing the result of our function. All that is left is to call wp_tag_cloud with those parameters and we are done.

Note: if you don’t use wp_tag_cloud to display the Tag Cloud widget in a template file, but instead use the default widget at Dashboard > Appearance > Widgets, check my other article on how to modify parameters on the default tag cloud widget.

Conclusion

WordPress offers us many parameters to customize tag cloud widget, but there doesn’t seem to be one that would exclude tags with specific count numbers. Luckily, we can use exclude parameter to achieve the same result. In this article, we created a function that loops through all the post tags, and adds those with low number of count values to the exclude list. This way, we can have in our tag cloud only the most popular tags. Finally, we showed, how to display a tag cloud with this modification using wp_tag_cloud() function.

Share this page

Add a Comment

Back to Top