Support » Fixing WordPress » using wp_tag_cloud() as array

  • Resolved roganty


    I created a Tag Archive Page and I am having trouble getting wp_tag_cloud() to work

    Heres the the code:

    if( is_page() ){
     $tagcloud = wp_tag_cloud('number=0&format=array&orderby=name&order=ASC');

    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)
  • I think the Template Tag, get_the_tags, might be more appropriate for you.

    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 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:

         [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 its still work in progress!

    Maybe I could turn it in to some sort of plugin after I’ve finished?

    Thanks for your help

    Okay how about get_tags?

    You’ve probably figured it out…

    $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;

    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.

    $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

    so… are there someone alive ?…

    This has already been fixed in the next version of WordPress.

    Also, your code above is wrong. It should be this:

    $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).


    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.