• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2785

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

Customize WordPress Tag Cloud Widget Functionality

This code enables you to modify the default functionality for the WordPress tag cloud widget. You’ll find the tag cloud widget by going to Appearance > Widgets.

The code uses the widget_tag_cloud_args filter which you can find around line 66 in wordpress > wp-includes > widgets > class-wp-widget-tag-cloud.php

Simply paste the following PHP code at the end of your child themes functions file.

add_filter( 'widget_tag_cloud_args', 'all_tag_cloud_widget_parameters' );
function all_tag_cloud_widget_parameters() {
    $args = array(
        'smallest' => 12, 
        'largest' => 18, 
        'unit' => 'pt', 
        'number' => 10,
        'format' => 'flat', 
        'separator' => "\n", 
        'orderby' => 'name', 
        'order' => 'ASC',
        'exclude' => '', 
        'include' => '', 
        'link' => 'view', 
        'taxonomy' => $current_taxonomy, 
        'post_type' => '', 
        'echo' => false
    );
    return $args;
}

Remove the arguments you do not want to modify from the array.

Example : If you only want to control which tags are included in the tag cloud widget, use the code like this:

add_filter( 'widget_tag_cloud_args', 'include_in_tag_cloud_widget' );
function include_in_tag_cloud_widget() {

    $include = array( 58, 59 );

    $args = array(
        'include' => $include,
        'taxonomy' => $current_taxonomy,
	    'echo' => false,     
    );
    return $args;
}

Where 58 and 59 are the i.d’s of the tags you want to include in the widget.

Exclude Tag by Name, I.D or Slug

You can also use the code like this to remove tags by name, id or slug using get_term_by :

add_filter( 'widget_tag_cloud_args', 'remove_tag' );

function remove_tag( $args ) {
    $args['exclude'] =  get_term_by( 'slug', 'your-tags-slug', 'post_tag' );
    return $args;
}

Swap out your-tags-slug in the above code with the slug for your tag.

Related Tutorials

  • No Follow WordPress Tag Cloud Widget Links

Reader Interactions

Comments

  1. Augusto says

    December 18, 2017 at 6:07 pm

    Hi Brad!

    I’m trying to change the default number of tags. When using this code in the child-theme functions file, nothing changes, but if added in the main theme functions file, it works. Any clues about that?

    Log in to Reply
    • Brad Dalton says

      December 19, 2017 at 1:15 am

      Try a 3rd parameter.

      Log in to Reply
  2. KONAN YAMADA says

    November 9, 2017 at 5:49 am

    Sorry, I cannot display the title in spite of inputting title field in the widget.
    please help me.

    Log in to Reply
    • Brad Dalton says

      November 9, 2017 at 7:50 am

      This works :

      add_filter( 'widget_tag_cloud_args', 'all_tag_cloud_widget_parameters' );
      function all_tag_cloud_widget_parameters() {
      
      $args = array(
          'smallest' => 10,
          'largest' => 10,
          'unit' => 'pt',
          'format' => 'list',
          'order' => ASC,
          );
      return $args;
      
      }
      Log in to Reply
      • KONAN YAMADA says

        November 9, 2017 at 8:56 am

        Hello, I can solve using following code.
        —————-
        function all_tag_cloud_widget_parameters($args) {
        $myargs = array(
        ‘smallest’ => 1,
        ‘largest’ => 1,
        ‘unit’ => ’em’,
        ‘format’ => ‘list’,
        ‘order’ => ASC,
        );
        $args = wp_parse_args($args, $myargs);
        return $args;
        }
        add_filter( ‘widget_tag_cloud_args’, ‘all_tag_cloud_widget_parameters’ );
        —————-
        I used function “wp_parse_args”, It worked fine that merged old parameters and new parameters before return.

        Log in to Reply
        • Brad Dalton says

          November 9, 2017 at 8:59 am

          The code is broken because it hasn’t been embedded correctly.

          Log in to Reply
  3. KONAN YAMADA says

    November 9, 2017 at 3:49 am

    Hello, I have a question.
    I implemented this code, then I can realise adjusting tag cloud parameter, but I cannot display the title.
    Currently, I use this code as a custom widget.
    Could you tell me how to display the title of tag list widget?

    Log in to Reply
    • Brad Dalton says

      November 9, 2017 at 3:51 am

      Add the title to the widget title field

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags