5

By default, the WordPress tag cloud widget has a set amount of 45 tags to display. This can be seen in the wp-includes/category-template.php file.

By default, the WooCommerce plugin which I have installed, and it's products tag cloud widget also resembles this.

How do I modify this amount from within my wp-content/themes/functions.php file, to display for example, only 15 product tags?

Here is what I have so far, but it is not working.

function custom_tag_cloud_widget($args) {
    $args['smallest'] = 8; //smallest tag
    $args['largest'] = 22; //largest tag
    $args['number'] = 15; //adding a 0 will display all tags
    $args['unit'] = 'pt'; //tag font unit
    return $args;
}

add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' );

When changing the number within the core wp-includes.php/category-template.php file to 15 however, it does work.

Obviously, I don't wish to edit any core files and am looking for an alternative solution.

Thanks.

  • Try setting a priority, like add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget', 9999 ); – Pieter Goosen Jul 20 '14 at 16:27
  • Unfortunately, setting a priority of 9999 does not work either. – user1752759 Jul 21 '14 at 3:49
6

Add the following to your theme's function.php. Default values are shown below, except changing 'number' from 45 to 15. Only the changed values need to be included, so you can either leave the default values or remove/comment out those lines.

For Wordpress Tag Cloud widget:

function custom_tag_cloud_widget() {
    $args = array(
        'smallest' => 8, 
        'largest' => 22, 
        'unit' => 'pt', 
        'number' => 15,
        'format' => 'flat', 
        'separator' => "\n", 
        'orderby' => 'name', 
        'order' => 'ASC',
        'exclude' => '', 
        'include' => '', 
        'link' => 'view', 
        'taxonomy' => 'post_tag', 
        'post_type' => '', 
        'echo' => true
    );
    return $args;
}
add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' );

For WooCommerce Product Tags widget:

function custom_woocommerce_tag_cloud_widget() {
    $args = array(
        'number' => 15,
        'taxonomy' => 'product_tag'
    );
    return $args;
}
add_filter( 'woocommerce_product_tag_cloud_widget_args', 'custom_woocommerce_tag_cloud_widget' );
  • I am not so sure this is the best approach. But if it works, it works, I guess... – Johannes Pille Jul 20 '14 at 20:41
  • Unfortunately it does not work either. The amount of product tags are still displaying 45. – user1752759 Jul 21 '14 at 3:48
  • Sorry, I see now it's only changing the default for the WP Tag Cloud widget, and not the WooCommerce Product Tags widget. – Gabriel Jul 21 '14 at 6:20
  • 2
    OK, updated answer with code for WooCommerce Product Tags widget. Please give that a try. – Gabriel Jul 21 '14 at 6:48
0

This thread was a starting point today. The changes I made to my website may correlate with those you would make in WooCommerce. I wanted more useful search terms to show up in the Product Tag Cloud than the old ones. My website has WP E-Commerce, old version. No answers turned up elsewhere, so went to the wp-includes/category-template.php file, searched for 45, changed to 60 and tested. It worked for Blog Tag Cloud, but not Product Tag Cloud. The code looked like this:

function wp_tag_cloud( $args = '' ) {
    $defaults = array(
        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
        'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
        'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
    );

In WP E-Commerce files, found in wp-content/plugins/wp-e-commerce/tagging_functions.php, a similar section (searched for 45).

function product_tag_cloud(){
    product_tag_init();
    $defaults = array(
        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 60,
        'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
        'exclude' => '', 'include' => ''

Changed 45 to 60, saved, and tested. It worked. The tag cloud increased to 60.

I changed the number back to 45 in the Blog Cloud and tested again. The Blog Cloud has 45 terms now, and the Product Tag Cloud has 60. No apparent conflicts.

I've been editing the tags in each product to remove old ones and emphasize new ones. The cloud is gradually adjusting - removing some terms, adding others.

0

I have found a simple solution, Place this hook in your function.php file. It works fine for me. You just change the number parameter value according to your need.

//Register tag cloud filter 
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;
}

Here is the source https://w3reign.com/change-tag-cloud-widget-limit-in-wordpress/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.