Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Using wp_tag_cloud(), how can I display a single tag cloud that incorporates both regular post tags and a custom taxonomy?

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

The following is a slightly modified version of the wp_tag_cloud() function:

function custom_wp_tag_cloud( $args = '' ) {
    $defaults = array(
        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
        'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
        'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
    );
    $args = wp_parse_args( $args, $defaults );

    $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags

    if ( empty( $tags ) )
        return;

    foreach ( $tags as $key => $tag ) {
        if ( 'edit' == $args['link'] )
            $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
        else
            $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
        if ( is_wp_error( $link ) )
            return false;

        $tags[ $key ]->link = $link;
        $tags[ $key ]->id = $tag->term_id;
    }

    $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args

    $return = apply_filters( 'wp_tag_cloud', $return, $args );

    if ( 'array' == $args['format'] || empty($args['echo']) )
        return $return;

    echo $return;
}

Use the taxonomy argument:

$args = array(
   'taxonomy' => array( 'post_tag', 'custom_taxonomy' )
);
custom_wp_tag_cloud( $args );
share|improve this answer
    
Doh! I didn't realise you could post two arguments to taxonomy... thanks :) –  Amanda Nov 2 '10 at 8:23
    
You are welcome. –  sorich87 Nov 2 '10 at 9:00
    
Actually, that produces an error (the taxonomy argument apparently only accepts a string, not an array): Warning: Illegal offset type in isset or empty –  Amanda Nov 3 '10 at 20:26
    
I modified my answer. –  sorich87 Nov 3 '10 at 23:18
2  
@Amanda: wp_tag_cloud() will accept an array as taxonomy argument in 3.1 –  sorich87 Nov 6 '10 at 23:50
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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