On the Bitvolution WordPress theme, the sidebar is quite small (only 220px) and the default WordPress tag cloud widget was producing tags that were clipped in a ugly manner. By default, the WordPress tag cloud widget has a maximum font size of 22px so I was looking for a way to reduce it.
Note: If you are not a theme editor, you might find it easier to just install a suitable tag cloud plugin, e.g. Configurable Tag Cloud (CTC).
The WordPress tag cloud widget already allows you to specify various options including the largest font size, e.g. <?php wp_tag_cloud('largest=18'); ?>
so we only need to create a new widget that overrides the default widget and then unregister the default widget so there aren’t two widgets with the same name in the “Available Widgets” dashboard page. We can register our own widget using register_sidebar_widget and we can unregister the default tag cloud widget using unregister_widget('WP_Widget_Tag_Cloud');
.
This is the code you need – put it in the functions.php file in your WordPress theme folder:
add_action("widgets_init", array('Tag_cloud_withLimitedFontSize', 'register')); /** Widget - Override the default WordPress tag cloud BUT cap the largest font size to 18 (instead of 22)\ because at 22 some tags don't fit in the sidebar. */ class Tag_cloud_withLimitedFontSize { function widget($args){ echo $args['before_widget']; echo $args['before_title'] . 'Tags' . $args['after_title']; echo wp_tag_cloud('largest=18'); echo $args['after_widget']; } function register() { register_sidebar_widget('Tag Cloud', array('Tag_cloud_withLimitedFontSize', 'widget')); unregister_widget('WP_Widget_Tag_Cloud'); } }
If there’s a better way to do this, please let me know.
[…] theme limits the font size in the tag cloud widget – see blog post. This is needed because the sidebar is so […]