WordPress tag cloud widget – limit maximum tags

tagIt’s already a while ago that I implemented it but since I edited it a couple of minutes ago, I thought about it again: the tag cloud widget is a handy one to show all your post tags. Click on a tag and all posts show up with that particular tag. The widget is also automatically populated by tags being used by you. But this seemed to grow and grow and I couldn’t find a option to limit this. Until now.

It’s a bit random after posts like this and this, or a little bit more CentOS specific, but this is what makes the Linux quest so exciting. Certainly at the start, you never know what you will encounter during the day.

Anyway, here we go!

Hack the tag widget

I found it on the net a while ago but I don’t know where or who to give credit. Anyway, it’s an easy edit, but you have to use your WordPress editor. So in WordPress go to ‘Appearance’ and then to ‘Editor’. Look up you themes functions.php and edit the below code to the bottom.

/******************************************************************
* Limit the number of tags displayed by Tag Cloud widget *
******************************************************************/
add_filter( 'widget_tag_cloud_args', 'tj_tag_cloud_limit' );
function tj_tag_cloud_limit($args){
// Check if taxonomy option of the widget is set to tags
if ( isset($args['taxonomy']) && $args['taxonomy'] == 'post_tag' ){
$args['number'] = 10; // Number of tags to show
}
return $args;
}

Replace the number with the number you’d like obviously. Save it and reload your home page. Should be good.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.