Tag Cloud from WordPress Categories

As of this post, wordpress does not have a built in function to make a tag cloud from a specfic category. This is one of the few ways it can be done.

The Method

Make a query for the category and then make a custom loop to go through all the posts from that category. Inside your custom loop echo out the tags from each post. Once thats done, don’t forget to reset the query; otherwise it cant be used again on the same page.

<ul>
    <?php
    query_posts('category_name=BLANK'); //Category query
    if (have_posts()) : while (have_posts()) : the_post(); // Custom loop
   
    if( get_the_tag_list() ){
        echo $posttags = get_the_tag_list('<li>','</li><li>','</li>'); //Tags from each post  
    }
   
    endwhile; endif; //End custom loop
   
    wp_reset_query(); //Reset query
    ?>
</ul>

Just put the category slug name of the category you want in where it says BLANK in category_name=BLANK and paste the code anywhere you want to have that category tag cloud.

Custom Category Tag Cloud Function

If you want it to be in a nice compact function, then put it in the functions.php file.

<?php function cat_tag_cloud( $category_in ){  ?>
    <ul>
        <?php
        $category = 'category_name=' . $category_in;
        query_posts( $category );
        if (have_posts()) : while (have_posts()) : the_post();
       
            if( get_the_tag_list() ){
            echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
            }
       
        endwhile; endif;
       
        wp_reset_query();
        ?>
    </ul>
<?php } ?>

Then call the function anywhere you want, while giving it the category slug name as a parameter.

<?php
   cat_tag_cloud( 'BLANK' ); //functions.php category specific tag cloud
?>

Comment if you see a way to improve this method.

5 Responses to Tag Cloud from WordPress Categories

  1. Netscout says:

    Thanks for the code. I’ve got a question: On my website, using your code, I only get a list of the tags but not a cloud. How can I do this?

    • Nils says:

      No problem :]

      In your css file:

      ul {text-decoration:none; }
      ul li {float:left; }
      ul li a {display:inline; }

      and add any other styling you may want to add after that

    • Nils_E says:

      I will make a tutorial on how to style a clean-looking tag cloud with this function.. coming soon

  2. Mike says:

    Thanks for this, look fwd to the tutorial.

  3. Karl says:

    Howdy, I’ll be stoked to see that tutorial too! Happy to pay the developer for a chunk of code that will help us make a pretty tag cloud for a specific directory!
    Please email and let me know if you’re still working on this or need some in$entive…

    -Karl

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>