How do I display a tag cloud under my post that only shows tags from that post?

I’ve be looking to add a tag cloud under my post that only shows tags pertaining to the post. The tags shown should be in a tag cloud format; the post tags that have more post tagged with it will be displayed in a bigger font.

I’ve tried adding the following code under my posts:

<?php wp_tag_cloud( array(

        'smallest' => 8,          // font size for the least used tag
        'largest'  => 22,         // font size for the most used tag
        'unit'     => 'px',       // font sizing choice (pt, em, px, etc)
        'number'   => 45,         // maximum number of tags to show
        'format'   => 'flat',     // flat, list, or array. flat = spaces between; list = in li tags; array = does not echo results, returns array
        'orderby'  => 'name',     // name = alphabetical by name; count = by popularity
        'order'    => 'ASC',      // starting from A, or starting from highest count
        'include'  => $post_id,         // ID's of tags to include, displays none except these
        'link'     => 'view',     // view = links to tag view; edit = link to edit tag
        'taxonomy' => 'post_tag', // post_tag, link_category, category - create tag clouds of any of these things
        'echo'     => true        // set to false to return an array, not echo it

    ) ); ?>

I was attempting to use the include array to call up the post id to take reference of the post tag. But it does not work. It shows all the tags in existence, instead of the tags that are specific to the post.

Does anyone have a solution. Please help.

1
1

First you need to get all the assigned tag id:s by calling wp_get_post_tags because the include parameter in wp_tag_cloud only works with tags id, Not page id. So when you have all the id:s put them in the include parameter within the wp_tag_cloud like this:

<?php
    // Get the assigned tag_id
    $tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );

    // Check if there is any tag_ids, if print wp_tag_cloud
    if ( $tag_ids ) {

        wp_tag_cloud( array(
            'unit'     => 'px',       // font sizing choice (pt, em, px, etc)
            'include'  => $tag_ids,   // ID's of tags to include, displays none except these
        ) );
    }
?>

I have also removed some parameters that do nothing ancestry different than the defaults, you only need to add custom parameters if you need to modify the array.

Leave a Comment