• Hey guys, I tried creating a custom tag cloud shortcode, but it’s not showing the tags on the front of my site. Here’s the code I’m dropping into my functions.php file:

    function wpb_tag_cloud() { 
    $tags = get_tags();
    $args = array(
        'smallest'                  => 10, 
        'largest'                   => 22,
        'unit'                      => 'px', 
        'number'                    => 10,  
        'format'                    => 'flat',
        'separator'                 => " ",
        'orderby'                   => 'count', 
        'order'                     => 'DESC',
        'show_count'                => 1,
        'echo'                      => false
    ); 
     
    $tag_string = wp_generate_tag_cloud( $tags, $args );
     
    return $tag_string; 
     
    } 
    // Add a shortcode so that we can use it in widgets, posts, and pages
    add_shortcode('wpb_popular_tags', 'wpb_tag_cloud'); 
     
    // Enable shortcode execution in text widget
    add_filter ('widget_text', 'do_shortcode');

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello Matt!

    I have tried the code you provided, and it works as expected. Please make sure you already have tags in your website.

    Don’t forget to copy the [wpb_popular_tags] shortcode in the place when you want to show the cloud of tags. You can past it on any page in your website or in a text widget from your dashboard in Appearance >> Widgets.

    Please try this and let me know if all is fine and working now. ??

    Thread Starter Matt Rittman

    (@mrittman)

    Hmmm very strange! Well thanks for letting me know it’s working on your end. It must be something to do with my theme maybe? I have two custom taxonomies and I know I’m using the right ones. Is there any way to debug it to see where the problem might be?

    Thread Starter Matt Rittman

    (@mrittman)

    Actually you know what, I don’t think the function wp_generate_tag_cloud allows for a custom taxonomy. So how would I edit the code to only pull tags from a specific taxonomy?

    Thread Starter Matt Rittman

    (@mrittman)

    Nevermind, I think I figured out a solution but using this code instead:

    function my_cloud() {
        if (function_exists('wp_tag_cloud'))
            return wp_tag_cloud(array(
            	'smallest'                  => 9, 
    		'largest'                   => 20,
    		'unit'                      => 'pt', 
    		'number'                    => 10,  
    		'format'                    => 'list',
    		'separator'                 => "\n",
    		'orderby'                   => 'name', 
    		'order'                     => 'ASC',
    		'link'                      => 'view', 
    		'taxonomy'                  => '',
    		'show_count'                => 0,
            ));
    }
    add_shortcode('popular_tags', 'my_cloud');
    • This reply was modified 6 years, 6 months ago by Matt Rittman.
    • This reply was modified 6 years, 6 months ago by Matt Rittman.
    Moderator bcworkz

    (@bcworkz)

    Nice! One thing though, wp_tag_cloud() echoes by default. Add 'echo'=> false, to your arguments array.

    I’m assuming you will also add a taxonomy value for your custom taxonomy.

    Thread Starter Matt Rittman

    (@mrittman)

    I don’t really understand the whole echo thing lol. I went ahead and added it to my arguments array, but it didn’t seem to change anything. What does it do that I’m not seeing? Yeah, I have a taxonomy, but I just removed it for the sake of this thread ??

    Thanks @bcworks!

    Moderator bcworkz

    (@bcworkz)

    When we echo from a shortcode handler, we cannot predict where the output will occur. It’s often not where the shortcode was placed. In your case it sounds like it didn’t make any difference, a fortunate happenstance. In any case, it’s a firm rule that shortcode handlers must return the desired output and not echo out. Something to keep in mind for your next shortcode ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Tag Cloud Shortcode’ is closed to new replies.