WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] using wp_tag_cloud() as array (10 posts)

  1. roganty
    Member
    Posted 6 years ago #

    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');
     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

  2. MichaelH
    Member
    Posted 6 years ago #

    I think the Template Tag, get_the_tags, might be more appropriate for you.

  3. roganty
    Member
    Posted 6 years ago #

    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:

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

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

    Thanks for your help

  4. MichaelH
    Member
    Posted 6 years ago #

    Okay how about get_tags?

  5. MichaelH
    Member
    Posted 6 years ago #

    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;
    }
    }
    ?>
  6. roganty
    Member
    Posted 6 years ago #

    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.

  7. phpbbx
    Member
    Posted 6 years ago #

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

  8. phpbbx
    Member
    Posted 6 years ago #

    so... are there someone alive ?...

  9. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

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

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

  10. phpbbx
    Member
    Posted 6 years ago #

    Otto42

    Think you a lot it works !!

Topic Closed

This topic has been closed to new replies.

About this Topic