Codex

Template Tags/wp tag cloud

Contents

Description

Available with WordPress Version 2.3, this template tag wp_tag_cloud 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 taxonony could be used as the basis of generating the cloud. That means, for example, that a cloud for posts categories can be presented to visitors.

Usage

<?php wp_tag_cloud$args ); ?>

Default Usage

 <?php $args = 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 ); ?>

By default, the usage shows:

  • smallest - The smallest tag (lowest count) is shown at size 8
  • largest - The largest tag (highest count) is shown at size 22
  • unit - Describes 'pt' (point) as the font-size unit for the smallest and largest values
  • number - Displays at most 45 tags
  • format - Displays the tags in flat (separated by whitespace) style
  • separator - Displays whitespace between tags
  • orderby - Order the tags by name
  • order - Sort the tags in ASCENDING fashion
  • exclude - Exclude no tags
  • include - Include all tags
  • link - view
  • taxonomy - Use post tags for basis of cloud
  • echo - echo the results

Parameters

smallest
(integer) (optional) The text size of the tag with the smallest count value (units given by unit parameter).
Default: 8
largest
(integer) (optional) The text size of the tag with the highest count value (units given by the unit parameter).
Default: 22
unit
(string) (optional) Unit of measure as pertains to the smallest and largest values. This can be any CSS length value, e.g. pt, px, em, %.
Default: 'pt'
number
(integer) (optional) The number of actual tags to display in the cloud. (Use '0' to display all tags.)
Default: 45
format 
(string) (optional) Format of the cloud display.
  • 'flat' (Default) tags are separated by whitespace defined by 'separator' parameter
  • 'list' tags are in UL with a class='wp-tag-cloud'
  • 'array' tags are in an array and function returns the tag cloud as an array for use in PHP Note: the array returned, rather than displayed, was instituted with Version 2.5.
separator
(string) (optional) The text/space between tags. Note: this parameter was introduced with Version 2.9.
Default: '/n' (whitespace)
orderby 
(string) (optional) Order of the tags. Valid values:
  • 'name' (Default)
  • 'count'
order 
(string) (optional) Sort order. Valid values - Must be Uppercase:
  • 'ASC' (Default)
  • 'DESC'
  • 'RAND' tags are in a random order. Note: this parameter was introduced with Version 2.5.
exclude 
(string) (optional) Comma separated list of tags (term_id) to exclude. For example, 'exclude=5,27' means tags that have the term_id 5 or 27 will NOT be displayed. Defaults to exclude nothing.
include 
(string) (optional) Comma separated list of tags (term_id) to include. For example, 'include=5,27' means tags that have the term_id 5 or 27 will be the only tags displayed. Defaults to include everything.
link 
(string) (optional) Set link to allow edit of a particular tag. Note: this parameter was introduced with Version 2.7. Valid values:
  • 'view' (Default)
  • 'edit'
taxonomy 
(string) (optional) Taxonomy to use in generating the cloud. Note: this parameter was introduced with Version 2.8.
  • 'post_tag' - (Default) Post tags are used as source of cloud
  • 'category' - Post categories are used to generate cloud
  • 'link_category' - Link categories are used to generate cloud
echo 
(boolean) (optional) Show the result or keep it in a variable. The default is true (display the tag cloud). Note: this parameter was introduced with Version 2.8. Valid values:
  • 1 (true) - default
  • 0 (false)

Examples

Cloud displayed under Popular Tags title

<?php if ( function_exists('wp_tag_cloud') ) : ?>
<li>
<h2>Popular Tags</h2>
<ul>
<?php wp_tag_cloud('smallest=8&largest=22'); ?>
</ul>
</li>
<?php endif; ?>

Cloud limited in size and ordered by count rather than name

<?php wp_tag_cloud('smallest=8&largest=22&number=30&orderby=count'); ?>

Cloud returned as array but not displayed

The variable $tag will contain the tag cloud for use in other PHP code

 <?php $tag = wp_tag_cloud('format=array' );?>

Creating a Tag Archive

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

  • 1. Create file with the contents below named tag.php.
  • 2. Upload file to your themes directory.
  • 3. This is optional only if you would like to have a link in your page navigation to the Tag archive, otherwise when clicking on a tag this template will be used.
    • Create a new blank page using this template, give this page the title Tag Archive.

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(); ?>"><?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.

Changelog

  • 2.9 : Added the separator parameter.
  • 2.8 : Added the taxonomy and the echo parameters.
  • 2.7 : Added the link parameter.
  • 2.5 :
    • Added 'RAND' order for the order parameter.
    • format=array returns an array.
  • Since: 2.3

Source File

wp_tag_cloud() is located in wp-includes/category-template.php.

Related

the_tags, tag_description, single_tag_title, wp_tag_cloud, wp_generate_tag_cloud, get_tags, get_the_tags, get_the_tag_list, get_tag_link

See also index of Function Reference and index of Template Tags.