I’m trying to exclude a tag from the wordpress tag cloud by it’s slug.
What I have after a whole Saturday of searching and trying is this:
$term = get_term_by('slug', 'widget', 'portfolio-tag');
$term_id = $term->term_id;
$string_id = "'" . $term_id . "'";
add_filter('widget_tag_cloud_args', 'my_widget_tag_cloud_args');
function my_widget_tag_cloud_args($args) {
$args['number'] = 0;
$args['largest'] = 18;
$args['smallest'] = 8;
$args['unit'] = 'pt';
$args['order'] = 'DESC';
$args['orderby'] = 'count';
$args['separator'] = ' ';
$args['exclude'] = print_r($string_id, true);
return $args;
}
I guess the above is not the best option i tried this afternoon, but it’s the last one.
The piece of code below returns the number 18 if I use print_r($term_id) to check it:
$term = get_term_by( 'slug', 'widget', 'portfolio-tag' );
$term_id = $term->term_id;
If I use $args['exclude'] = '18'; the tag named ‘widget’ gets exclude. The only thing I still haven’t managed is to do something like this: $args['exclude'] = $term_id;
I know that the exclude parameter only accepts strings. The reason why I not just put 18 in there is because I like to share my theme with some artist-friends.
I’ve made a theme to show my paintings. I also made a widget that will show a random painting with the name ‘widget’. Only it’s useless to have that in the tag cloud. Believe it or not, but compared to my artist friends I’m by far the computer expert 😉 So I want to keep it as simple as possible for them, just put the widget-tag to a painting and the rest will be taken care of – at least, that’s the plan.
The main problem I can spot here is related to variable scope:
The
$string_idyou set is not available in themy_widget_tag_cloud_args()because you set it outside of that function.So you need to move the code that generates it inside there or find another way to do the job, like using an anonymous function or by using an object for this.
The other problems I’ve spotted are the use of
print_rand then you wrap the actual ID inside single quotes. That is both not necessary. In PHP an integer number and a string are normally just interchangeable. If not, just cast: