Wordpress MU: aggregare le singole tag cloud dei blog

lunedì 6 aprile, 2009

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():

PHP:
  1. /**
  2.  * Display tag cloud.
  3.  *
  4.  * The text size is set by the 'smallest' and 'largest' arguments, which will
  5.  * use the 'unit' argument value for the CSS text size unit. The 'format'
  6.  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
  7.  * 'format' argument will separate tags with spaces. The list value for the
  8.  * 'format' argument will format the tags in a UL HTML list. The array value for
  9.  * the 'format' argument will return in PHP array type format.
  10.  *
  11.  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
  12.  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
  13.  *
  14.  * The 'number' argument is how many tags to return. By default, the limit will
  15.  * be to return the top 45 tags in the tag cloud list.
  16.  *
  17.  * The 'topic_count_text_callback' argument is a function, which, given the count
  18.  * of the posts  with that tag, returns a text for the tooltip of the tag link.
  19.  * @see default_topic_count_text
  20.  *
  21.  * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
  22.  * function. Only one should be used, because only one will be used and the
  23.  * other ignored, if they are both set.
  24.  *
  25.  * @since 2.3.0
  26.  *
  27.  * @param array|string $args Optional. Override default arguments.
  28.  * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
  29.  */
  30. function wp_tag_cloud( $args = '' ) {
  31.     $defaults = array(
  32.         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  33.         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
  34.         'exclude' => '', 'include' => '', 'link' => 'view'
  35.     );
  36.     $args = wp_parse_args( $args, $defaults );
  37.  
  38.     $tags = get_tags( array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
  39.  
  40.     if ( empty( $tags ) )
  41.         return;
  42.  
  43.     foreach ( $tags as $key => $tag ) {
  44.         if ( 'edit' == $args['link'] )
  45.             $link = get_edit_tag_link( $tag->term_id );
  46.         else
  47.             $link = get_tag_link( $tag->term_id );
  48.         if ( is_wp_error( $link ) )
  49.             return false;
  50.  
  51.         $tags[ $key ]->link = $link;
  52.         $tags[ $key ]->id = $tag->term_id;
  53.     }
  54.  
  55.     $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
  56.  
  57.     $return = apply_filters( 'wp_tag_cloud', $return, $args );
  58.  
  59.     if ( 'array' == $args['format'] )
  60.         return $return;
  61.  
  62.     echo $return;
  63. }

Se analizziamo il codice ci accorgiamo che i punti cruciali sono: recupero delle tag

PHP:
  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:

PHP:
  1. /**
  2.  * Aggrega le singole tag cloud di tutti i blog
  3.  *
  4.  * @return
  5.  * @param object $args[optional]
  6.  */
  7. function wpmu_tag_cloud( $args = '' ) {
  8.     $defaults = array(
  9.         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  10.         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
  11.         'exclude' => '', 'include' => '', 'link' => 'view'
  12.     );
  13.     $args = wp_parse_args( $args, $defaults );
  14.    
  15.     // ottiene la lista di tutti i blog
  16.     $blog_list = get_blog_list(0, 9999);
  17.    
  18.     $tags = array();
  19.  
  20.     // per ognuno dei blog recupera le tag        
  21.     foreach( $blog_list as $key => $blog ) {
  22.         $blog_id = $blog['blog_id'];
  23.         // seleziona il blog
  24.         switch_to_blog($blog_id);
  25.         $single_blog_tags     = get_tags( array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
  26.    
  27.         foreach ( $single_blog_tags as $key => $tag ) {
  28.             if ( 'edit' == $args['link'] )
  29.                 $link = get_edit_tag_link( $tag->term_id );
  30.             else
  31.                 $link = get_tag_link( $tag->term_id );
  32.             if ( is_wp_error( $link ) ) return false;
  33.    
  34.             $single_blog_tags[ $key ]->link = $link;
  35.             $single_blog_tags[ $key ]->id = $tag->term_id;
  36.         }
  37.  
  38.         // fonde le singole tag list in una lista unica
  39.         $tags = array_merge($single_blog_tags, $tags);
  40.     }
  41.  
  42.     // richiama la funzione standard di Wordpress
  43.     $return = wp_generate_tag_cloud( $tags, $args );
  44.  
  45.     $return = apply_filters( 'wp_tag_cloud', $return, $args );
  46.  
  47.     if ( 'array' == $args['format'] )
  48.         return $return;
  49.  
  50.     echo $return;
  51. }

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à.

Post correlati

Questo articolo ti è stato utile?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

6 commenti a: “Wordpress MU: aggregare le singole tag cloud dei blog”

  1. 06 mag, 2009 aldo:

    in che file risiede questa funzione ?

  2. 06 mag, 2009 Giovambattista Fazioli:

    @aldo:

    in che file risiede questa funzione ?

    Si trova nella cartella wp-includes nel file category-template.php riga 552 :)

  3. 06 mag, 2009 aldo:

    Ho 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?

  4. 06 mag, 2009 Giovambattista Fazioli:

    @aldo:

    ma mi da errore

    che tipo di errore ti da e dove?

  5. 06 mag, 2009 aldo:
    Fatal error: Call to undefined function wp_tag_cloud()
    in /home/content/b/a/s/bastaldo/html/wp-content/themes/FREEmium/sidebar.php
    on line 28

    me lo da li dove dovrebbero esserci i tag

  6. 08 mag, 2009 Giovambattista Fazioli:

    @aldo: sembrerebbe che non hai disponibile quella funzione! Che versione di Wordpress usi?

Lascia un commento

TAG XHTML PERMESSI: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERIMENTO CODICE:
<pre></pre>         // blocco generico
[code][/code]       // blocco generico
[as][/as]           // Actionscript
[css][/css]         // CSS Style Sheet
[html][/html]       // HTML
[js][/js]           // Javascript
[objc][/objc]       // Objective-C
[php][/php]         // PHP
[sql][/sql]         // SQL