Title: using wp_tag_cloud() as array
Last modified: August 18, 2016

---

# using wp_tag_cloud() as array

 *  Resolved [roganty](https://wordpress.org/support/users/roganty/)
 * (@roganty)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/)
 * I created a [Tag Archive Page](http://codex.wordpress.org/Template_Tags/wp_tag_cloud#Creating_a_Tag_Archive)
   and I am having trouble getting [wp_tag_cloud()](http://codex.wordpress.org/Template_Tags/wp_tag_cloud)
   to work
 * Heres the the code:
 *     ```
       if( is_page() ){
        $tagcloud = wp_tag_cloud('number=0&format=array&orderby=name&order=ASC');
        print_r($tagcloud);
       }
       ```
   
 * The problem is that ‘Array’ is printed on the screen, if the format is array 
   shouldn’t the value be returned and not echo’d?
 * Btw, commenting out the print_r() function still prints ‘Array’ to the screen.
 * Is this the correct way to use wp_tag_cloud()?
 * Thanks for any help

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647355)
 * I think the [Template Tag](http://codex.wordpress.org/Template_Tags), [get_the_tags](http://codex.wordpress.org/Template_Tags/get_the_tags),
   might be more appropriate for you.
 *  Thread Starter [roganty](https://wordpress.org/support/users/roganty/)
 * (@roganty)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647359)
 * MichaelH, thanks for your reply.
 * get_the_tags() is not what I am looking for, as I want to create a list of all
   tags used, but I don’t want any formating.
 * I have found [this](http://trac.wordpress.org/ticket/5155) on Trac and have applied
   the patch, but it still isn’t what I am trying to do
 * What I was hoping it would do is return the array in the following format:
 *     ```
       Array
       (
            [0] => Array
                 (
                      [term_id] => 1
                      [name] => Slug
                      [slug] => slug
                      [count] => 5
                 )
       )
       ```
   
 * At the moment I’m using get_tags() to get the tags, then using some parts of 
   wp_generate_tag_cloud() to create the list
    [http://www.roganty.co.uk/blog/tag](http://www.roganty.co.uk/blog/tag)
   its still work in progress!
 * Maybe I could turn it in to some sort of plugin after I’ve finished?
 * Thanks for your help
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647409)
 * Okay how about get_tags?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647448)
 * You’ve probably figured it out…
 *     ```
       <?php
       $mytags = get_tags() ;
       if ($mytags) {
       foreach($mytags as $tag) {
       echo '<br /> id ' . $tag->term_id;
       echo '<br />name ' . $tag->name;
       echo '<br />slug ' . $tag->slug;
       echo '<br />count ' . $tag->count;
       }
       }
       ?>
       ```
   
 *  Thread Starter [roganty](https://wordpress.org/support/users/roganty/)
 * (@roganty)
 * [18 years, 7 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647459)
 * I have, thanks MichaelH
 * Here’s the code I am using:
 *     ```
       $tagcloud = get_tags(array('number' => 150, 'orderby' => 'count', 'order' => 'DESC', 'exclude' => '', 'include' => ''));
   
       $counts = $tag_links = $tag_ids = $a = array();
   
       foreach( $tagcloud as $tag ){
        $counts[$tag->name] = $tag->count;
        $tag_links[$tag->name] = get_tag_link($tag->term_id);
        $tag_ids[$tag->name] = $tag->term_id;
       }
   
       uksort($counts, 'strnatcasecmp');
   
       foreach( $counts as $tag=>$count ){
        $tag_id = $tag_ids[$tag];
        $tag_link = clean_url($tag_links[$tag]);
        $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
        $a[] = "<a href=\"$tag_link\" class=\"tag-link-$tag_id\">$tag</a> ($count)"; # title=\"" .attribute_escape( sprintf( __('%d posts'), $count ) ). "\"
       }
   
       print "<ul class='wp-tag-cloud'>\r\n<li>";
       print join("</li>\r\n<li>", $a);
       print "</li>\r\n</ul>\r\n";
       ```
   
 * Some of it is a direct copy from wp_generate_tag_cloud() in /wordpress/wp-includes/
   category-template.php
 * Thanks for your help.
 *  [phpbbx](https://wordpress.org/support/users/phpbbx/)
 * (@phpbbx)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647591)
 * <?php
    $mytags = get_tags() ; if ($mytags) { foreach((get_the_tags()) as $tag){
   echo ‘ id ‘ . $tag->term_id; echo ‘name ‘ . $tag->name; echo ‘slug ‘ . $tag->
   slug; echo ‘count ‘ . $tag->count; } } ?>
 * that’s good … but if there s no tag it give Error
 * it shoud be fixed by some else echo’nothing’ or somthing like that … waiting 
   for a coder
 *  [phpbbx](https://wordpress.org/support/users/phpbbx/)
 * (@phpbbx)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647592)
 * so… are there someone alive ?…
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 4 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647593)
 * This has already been fixed in the next version of WordPress.
 * [http://trac.wordpress.org/ticket/5155](http://trac.wordpress.org/ticket/5155)
 * Also, your code above is wrong. It should be this:
 *     ```
       <?php
       $mytags = get_the_tags() ;
       if (!empty($mytags)) {
       foreach($mytags as $tag) {
       echo ' id ' . $tag->term_id;
       echo 'name ' . $tag->name;
       echo 'slug ' . $tag->slug;
       echo 'count ' . $tag->count;
       }
       }
       ?>
       ```
   
 * get_tags returns *all* the tags.
    get_the_tags returns tags for the current post
   only (and should only be used in The Loop).
 *  [phpbbx](https://wordpress.org/support/users/phpbbx/)
 * (@phpbbx)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647594)
 * Otto42
 * **Think you a lot it works !!**

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘using wp_tag_cloud() as array’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [page](https://wordpress.org/support/topic-tag/page/)
 * [Tag Archive](https://wordpress.org/support/topic-tag/tag-archive/)
 * [wp_tag_cloud](https://wordpress.org/support/topic-tag/wp_tag_cloud/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 4 participants
 * Last reply from: [phpbbx](https://wordpress.org/support/users/phpbbx/)
 * Last activity: [18 years, 4 months ago](https://wordpress.org/support/topic/using-wp_tag_cloud-as-array/#post-647594)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
