Customizing The Tag Cloud In WordPress

Displaying categories and tags in a widget in WordPress is quite easy, just drag and drop the tag cloud widget to any active sidebar area and you are done. But customizing it is very tough, mainly because there are no options for it in the WordPress dashboard.

But WordPress allows theme developers to customize the tag cloud’s appearance and functionality. You have to paste a simple code snippet to the functions.php file of the active theme to get the desired look and feel of the tag cloud.

add_filter('widget_tag_cloud_args','style_tag_cloud'); function style_tag_cloud($args) {     $args = array(         'smallest' => 14,         'largest' => 18,         'unit' => 'px',         'number' => 20,         'format' => 'list',         'orderby' => 'name',         'order' => 'ASC',         'exclude' => 2,9,         'include' => null,     );     return $args; }

Add the above code to your active theme’s functions.php file. There are some parameters that need to be changed though. I will explain each of them in detail.

  1. smallest: This parameter is used to specify the minimum text size of the tags shown in the widget. You need to specify only the numerical value, the unit needs to be passed separately. Default value for this parameter is ‘8’.
  2. largest: Like smallest, largest carries the maximum text size that can be used by any tag. ’22’ is the default value for this parameter.
  3. unit: This parameter takes the unit for the values specified in the smallest and the largest parameter. It can be set to ‘pt’, ‘px’, ’em’ or ‘%’. Default is the ‘pt’.
  4. number: You can tell WordPress the number of tags you want to be displayed in the tag cloud. The default number of tags are ’45’, you can display all the available tags by passing the ‘0’ value in the parameter.
  5. format: The format argument is used to format the tag cloud. The argument can take three different values.

    • ‘flat’ – Only separated by whitespace
    • ‘list’ – Unordered list with ‘wp-tag-cloud‘ class
    • ‘array’ – An array that can be used in PHP.

    The default value for this argument is ‘flat’, but ‘list’ is recommended for better customization.

  6. orderby: This parameter is used to order the tags in the tag cloud. You can sort the tags according to the name (‘name’) or the number of posts with that tag/category (‘count’). The default value for this parameter is ‘name’.
  7. order: Orderby is used to order the tags according to a certain value, but order is given to specify if the tags are to be sorted in the ascending (‘ASC’), descending (‘DESC’), or random (‘RAND’) order. Default is ascending.
  8. exclude: You can exclude certain tags/categories by passing their IDs within the exclude parameter. The IDs should be separated by commas (,). The tag cloud excludes no tag by default.
  9. include: This argument is used if you want to display only some specific tags in the tag cloud. Pass the comma separated list of the IDs for the tags you want to display, then only those tags will be displayed in the tag cloud.

There are some other parameters that can be passed, but are of very less or no use. You can also customize the tag cloud in a better way if you use the unordered list as the format.

Related Post