Wordpress MU: aggregare le singole tag cloud dei blog
lunedì 6 aprile, 2009Potrebbe 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():
-
/**
-
* 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 = '' ) {
-
'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
-
-
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
-
$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:
-
/**
-
* Aggrega le singole tag cloud di tutti i blog
-
*
-
* @return
-
* @param object $args[optional]
-
*/
-
function wpmu_tag_cloud( $args = '' ) {
-
'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);
-
-
-
// 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
-
}
-
-
// 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à.










19

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?