Limit the Number of Tags in WordPress Tag Cloud Widget

If you are looking to limit the number of tags in WordPress tag cloud widget, this blog post is very helpful for you. Tag cloud widget is WordPress core widget which helps you to show the tags on your WordPress website. The tag is one of the predefined taxonomy of WordPress where users can add their own tag like category via Post section of admin dashboard. Users can add the Tags while creating the post as well.

Tags Widget

If you are using Tag Cloud widget to show the available Tags on your WordPress website and due to a large number of tags your site looks weird and messy you can easily limit the number of tags to display there.

Follow the below-listed method to limit the number of tags,

  1. Open your theme `functions.php` file (Make sure you are editing activated theme file)
  2. Paste the below code at the end of your functions.php file
  3. Save the file.
//Register tag cloud filter callback
add_filter('widget_tag_cloud_args', 'tag_widget_limit');
//Limit number of tags inside widget
function tag_widget_limit($args){
//Check if taxonomy option inside widget is set to tags
if(isset($args['taxonomy']) && $args['taxonomy'] == 'post_tag'){
$args['number'] = 10; //Limit number of tags
}
return $args;
}

You can change the number on the section//Limit number of tags and check on the front view. Hope this code helps you to limit the number of Tags on your website. We recommend you to use Child Theme for such theme customization. Check this blog to know the Importance of Child Theme.

WooCommerce Product Tag Cloud

If you want to change the number of tags on WooCommerce Product Tag Cloud Widget, as per the above method, add the below code snippit on functions.php file,


function custom_woocommerce_tag_cloud_widget() {
    $args = array(
        'number' => 5,
        'taxonomy' => 'product_tag'
    );
    return $args;
}
add_filter( 'woocommerce_product_tag_cloud_widget_args', 'custom_woocommerce_tag_cloud_widget' );

Hope this post is very helpful, please comment your feedback or suggestion in the comment section below.

Written by