Potrebbe non capitarvi mai di dover aggregare le singole tag cloud in WordPress MU. Inoltre, in determinati contesti, potrebbe non aver senso mostrare una “nuvola” con le tag di blog eterogenei. Tuttavia, nel caso remoto che a qualcuno servisse, ecco un modo semplice di mostrare una tag cloud aggregata.
La tag cloud standard di WordPress
Prima di tutto andiamo a vedere cosa usa WordPress per visualizzare la tag cloud standard: la funzione wp_tag_cloud():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /** * Display tag cloud. * * The text size is set by the 'smallest' and 'largest' arguments, which will * use the 'unit' argument value for the CSS text size unit. The 'format' * argument can be 'flat' (default), 'list', or 'array'. The flat value for the * 'format' argument will separate tags with spaces. The list value for the * 'format' argument will format the tags in a UL HTML list. The array value for * the 'format' argument will return in PHP array type format. * * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'. * * The 'number' argument is how many tags to return. By default, the limit will * be to return the top 45 tags in the tag cloud list. * * The 'topic_count_text_callback' argument is a function, which, given the count * of the posts with that tag, returns a text for the tooltip of the tag link. * @see default_topic_count_text * * The 'exclude' and 'include' arguments are used for the {@link get_tags()} * function. Only one should be used, because only one will be used and the * other ignored, if they are both set. * * @since 2.3.0 * * @param array|string $args Optional. Override default arguments. * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. */ function wp_tag_cloud( $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC', 'exclude' => '', 'include' => '', 'link' => 'view' ); $args = wp_parse_args( $args, $defaults ); $tags = get_tags( 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 ); else $link = get_tag_link( $tag->term_id ); 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'] ) return $return; echo $return; } |
Se analizziamo il codice ci accorgiamo che i punti cruciali sono: recupero delle tag
1 | $tags = get_tags( array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags |
L’array $tags viene poi utilizzato dalla funzione wp_generate_tag_cloud() che genera effettivamente la “nuvola”.
Modifica per WordPress MU
Modificando leggermente questa funzione, possiamo ottenere una nuvola che comprende tutti i tag dei nostri blog:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /** * Aggrega le singole tag cloud di tutti i blog * * @return * @param object $args[optional] */ function wpmu_tag_cloud( $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC', 'exclude' => '', 'include' => '', 'link' => 'view' ); $args = wp_parse_args( $args, $defaults ); // ottiene la lista di tutti i blog $blog_list = get_blog_list(0, 9999); $tags = array(); // per ognuno dei blog recupera le tag foreach( $blog_list as $key => $blog ) { $blog_id = $blog['blog_id']; // seleziona il blog switch_to_blog($blog_id); $single_blog_tags = get_tags( array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags foreach ( $single_blog_tags as $key => $tag ) { if ( 'edit' == $args['link'] ) $link = get_edit_tag_link( $tag->term_id ); else $link = get_tag_link( $tag->term_id ); if ( is_wp_error( $link ) ) return false; $single_blog_tags[ $key ]->link = $link; $single_blog_tags[ $key ]->id = $tag->term_id; } // fonde le singole tag list in una lista unica $tags = array_merge($single_blog_tags, $tags); } // richiama la funzione standard di Wordpress $return = wp_generate_tag_cloud( $tags, $args ); $return = apply_filters( 'wp_tag_cloud', $return, $args ); if ( 'array' == $args['format'] ) return $return; echo $return; } |
Il cuore di questa variante risiede nella funzione switch_to_blog(). Grazie a questa funzione è possibile scorrere tutti i blog ed estrarre le singolo tag cloud. Fatto questo tramite array_merge() si fondono le singole “nuvole” in un’unica nuvola da passare alla funzione wp_generate_tag_cloud(), che elaborerà l’array come una singola entità.








7
in che file risiede questa funzione ?
@aldo:
Si trova nella cartella
wp-includesnel filecategory-template.phpriga 552Ho provato a sostituire il codice con quello da te scritto ma mi da errore, appena ho un pò di tempo provo a modificarlo senza incollare cosi a casaccio. Volevo farti un altra domanda, che usi per la sitemap di tutti i blog del WPMU? Secondo te esiste qualche plugin che genera in automatico un unica sitemap anche per i blog degli utenti?
@aldo:
che tipo di errore ti da e dove?
me lo da li dove dovrebbero esserci i tag
@aldo: sembrerebbe che non hai disponibile quella funzione! Che versione di WordPress usi?
mi è stata utilissima!
grazie
[...] Dopo l’ottima guida trovata per visualizzare i post dei vari siti del network , oggi vorrei segnalare come visualizzare la tag cloud di tutti i blog del network. L’autore è Undolog e la guida la potete trovare a questa pagina. [...]
Nel caso un tag sia presente in n siti del network, nella tag cloud vengono riproposti n volte. Secondo te è possibile che vengano sommati i tag di siti differenti?
Luca
@BECA: possibilissimo, anzi. Come si vede dal loop della funzione, i tag vengono estratti per ogni blog. Quindi può benissimo capitare che lo stesso tag sia presente in uno o più blog. Ovviamente questa ridondanza è solo apparente in quanto quel tag (quei tag) puntano a contesti e contenuti differenti.
Quello che si potrebbe fare è migliorare la funzione. Ad esempio se il tag “Apple” è presente su diversi blog, sarebbe bene emetterlo tutte le volte che compare, indicando però che appartiene a blog differenti. L’ideale sarebbe avere l’indicazione del tipo “Apple (nome blog)” …
In pratica il loop ad ogni estrazione di tag dovrebbe creare una lista tag-blog, controllare se ci sono duplicati e in quel caso emetterlo nella forma “tag (nome blog)” … in modo che
array_merge()non combini pasticci!