Tag Cloud Shortcode
Adds a [tag_cloud] shortcode to show a tag cloud inside a page or post, with control over how many tags appear and how large they get.
Opens WPIDE on
Code
add_shortcode( 'tag_cloud', function ( $atts ) {
$atts = shortcode_atts( array(
'number' => 45,
'smallest' => 0.8,
'largest' => 1.6,
'taxonomy' => 'post_tag',
'orderby' => 'name',
), $atts, 'tag_cloud' );
$cloud = wp_tag_cloud( array(
'number' => absint( $atts['number'] ),
'smallest' => (float) $atts['smallest'],
'largest' => (float) $atts['largest'],
'unit' => 'em',
'taxonomy' => taxonomy_exists( $atts['taxonomy'] ) ? $atts['taxonomy'] : 'post_tag',
'orderby' => 'count' === $atts['orderby'] ? 'count' : 'name',
'order' => 'count' === $atts['orderby'] ? 'DESC' : 'ASC',
'echo' => false,
) );
return $cloud ? '<div class="wpide-tag-cloud">' . $cloud . '</div>' : '';
} );
Notes
Example: [tag_cloud number="30" smallest="0.8" largest="1.6" taxonomy="post_tag"]. Sizes are in em.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.