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.
Leave a Reply
You must be logged in to post a comment.