Comment afficher un nuage de tags avec les deux balises de poste et une taxonomie personnalisée?
-
16-10-2019 - |
Question
Utilisation wp_tag_cloud()
, comment puis-je afficher un nuage de tags qui intègre les deux balises post réguliers et une taxonomie personnalisée?
La solution
Ce qui suit est une version légèrement modifiée de la fonction de wp_tag_cloud()
:
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;
}
Utilisez l'argument taxonomy
:
$args = array(
'taxonomy' => array( 'post_tag', 'custom_taxonomy' )
);
custom_wp_tag_cloud( $args );
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange