2

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

5

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 );
  • Doh! I didn't realise you could post two arguments to taxonomy... thanks :) – Amanda Nov 2 '10 at 8:23
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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