Languages: English • Français • Italiano • 日本語 (Add your language)
The wp_tag_cloud() function displays a list of tags in what is called a 'tag cloud', where the size of each tag is determined by how many times that particular tag has been assigned to posts.
Beginning with Version 2.8, the taxonomy parameter was added so that any taxonomy could be used as the basis of generating the cloud. That means that a cloud for Categories or any other Custom Taxonomies can be presented to visitors.
<?php wp_tag_cloud( $args ); ?>
<?php $args = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', 'exclude' => null, 'include' => null, 'topic_count_text_callback' => default_topic_count_text, 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true, 'show_count' => 0, 'child_of' => null, // see Note! ); ?>
The child_of Key is not a direct part of the Array in wp_tag_cloud, but because this function uses wp_parse_args() and get_terms(), you can use all Array Keys used by get_terms()!
By default, the usage shows:
<?php if ( function_exists( 'wp_tag_cloud' ) ) : ?> <h2>Popular Tags</h2> <ul> <li><?php wp_tag_cloud( 'smallest=8&largest=22' ); ?></li> </ul> <?php endif; ?>
<?php wp_tag_cloud( 'smallest=15&largest=40&number=50&orderby=count' ); ?>
The variable $tag will contain the tag cloud for use in other PHP code
<?php $tag = wp_tag_cloud( 'format=array' ); ?>
Use the taxonomy argument to cause a cloud of categories to display.
<?php wp_tag_cloud( array( 'taxonomy' => 'category' ) ); ?>
Use the array feature of the taxonomy argument to cause a cloud of categories and tags to display.
<?php $args = array( 'taxonomy' => array( 'post_tag', 'category' ), ); wp_tag_cloud( $args ); ?>
Use the topic_count_text_callback argument to pass in a new callback function. The original function default_topic_count_text() is located in /wp-includes/category-template.php This example changes the title text from the default "topics" to "pictures".
<?php wp_tag_cloud( array( 'topic_count_text_callback' => 'my_tag_text_callback' ) ); function my_tag_text_callback( $count ) { return sprintf( _n( '%s picture', '%s pictures', $count ), number_format_i18n( $count ) ); } ?>
While the new tagging feature in 2.3 is a great addition, the wp_tag_cloud tag can be used to display a Tag Archive. What this means is that when a visitor clicks on any particular tag a page displaying the tag cloud and all posts tagged the same will be displayed. According to the Template_Hierarchy if a tag.php template does not exist then the archive.php template will be used. By making this tag.php template you can customize the way your Tag Archive will look, this template includes the tag cloud at the top for very easy navigation.
To do this a new template will need to be added to your theme files. These are good resources for everything pertaining to templates, Template_Hierarchy. Basic steps needed are
To elaborate more on step three:
WordPress can be configured to use different Page Templates for different Pages. Toward the bottom of the Write->Write Page administration panel (or on the sidebar, depending on which version of WordPress you are using) is a drop-down labeled "Page Template". From there you can select which Template will be used when displaying this particular Page.
<?php /* Template Name: Tag Archive */ ?> <div> <?php get_header(); ?> <h2>Tag Archive</h2> <?php wp_tag_cloud( '' ); ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link( '« Older Entries' ); ?></div> <div class="alignright"><?php previous_posts_link( 'Newer Entries »' ); ?></div> </div> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> <div class="entry"> <?php the_content( 'Read the rest of this entry »' ); ?> </div> <?php endwhile; ?> <?php endif; ?> </div> <?php get_footer(); ?>
Please Note that styling has not been added to this template. A good way to determine the structure that your theme uses is to view the single.php theme file.
taxonomy
parameter.separator
parameter.taxonomy
parameter.echo
parameter.link
parameter.'RAND'
order for the order
parameter to return tags in a random order.'array'
format for the format
parameter to return an array.wp_tag_cloud() is located in wp-includes/category-template.php
.
Tags: get_tag(), get_tag_link(), get_tags(), get_the_tag_list(), get_the_tags(), is_tag(), the_tags(), single_tag_title(), tag_description(), wp_generate_tag_cloud(), wp_tag_cloud()