6

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 Answer 1

7

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.

5
  • Hi Pontus, thanks. I implemented the code and tested it. It works! However, with posts that I have not added any tags, all existing tags show up as default. What array should I edit to prevent this from happening?
    – Logen
    Commented Jul 5, 2012 at 8:17
  • 1
    I have updated the code to check if there is any tag_id, if continue and print the wp_tag_cloud. Maybe there is some better solution for this. if so please add it Commented Jul 5, 2012 at 8:23
  • +1 for "you only need to add custom parameters if you need to modify the array."
    – kaiser
    Commented Jul 5, 2012 at 9:34
  • Thanks Pontus for the help and pointers about custom parameters.
    – Logen
    Commented Jul 5, 2012 at 22:26
  • 1
    @Logen Im glad that i could help you! Commented Jul 6, 2012 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.