Per creare una tag cloud in WordPress solo con i tag di alcune categorie occorre utilizzare una soluzione specifica.
Possiamo creare la seguente funzione in functions.php
:
function my_tag_cloud( $echo = false ) {
// Il parametro da modificare solo gli ID delle categorie
$args = array( 'posts_per_page' => -1, 'category__in' => array( 1, 2, 3 ) );
$loop = new WP_Query( $args );
$tags = array();
while( $loop->have_posts() ) {
$loop->the_post();
$id = get_the_ID();
$tag_ids = wp_get_post_tags( $id, array( 'fields' => 'ids' ) );
foreach( $tag_ids as $tag_id ) {
$tags[] = $tag_id;
}
}
wp_reset_postdata();
$tags_in = array_unique( $tags ); // Eliminiamo i duplicati
$tags_arg = array( 'echo' => false, 'include' => implode(',', $tags_in ) );
$html = '<div class="tagcloud">' . wp_tag_cloud( $tags_arg ) . '</div>';
if( $echo ) {
echo $html;
} else {
return $html;
}
}