WordPress:Template Tags/wp tag cloud

来自站长百科
Xxf3325讨论 | 贡献2008年6月27日 (五) 11:26的版本 (新页面: == 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', wh...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

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.

Usage

<?php wp_tag_cloud(''); ?>

Examples

Default Usage

$defaults = array('smallest' => 8, 'largest' => 22,
  'unit' => 'pt', 'number' => 45, 'format' => 'flat', 
  'orderby' => 'name', 'order' => 'ASC','exclude' => , 'include' => );

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
  • orderby - Order the tags by name
  • order - Sort the tags in ASCENDING fashion
  • exclude - Exclude no tags
  • include - Include all tags

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' );?>

Parameters

smallest
(integer) The text size of the tag with the smallest count value (units given by unit parameter).
largest
(integer) The text size of the tag with the highest count value (units given by the unit parameter).
unit
(string) Unit of measure as pertains to the smallest and largest values. This can be any CSS length value, e.g. pt, px, em, %; default is pt (points).
number
(integer) The number of actual tags to display in the cloud. (Use '0' to display all tags.)
format
(string) Format of the cloud display.
  • 'flat' (Default) tags are separated by whitespace
  • '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 WordPress:Version 2.5.
orderby
(string) Order of the tags. Valid values:
  • 'name' (Default)
  • 'count'
order
(string) Sort order. Valid values - Must be Uppercase:
  • 'ASC' (Default)
  • 'DESC'
  • 'RAND' tags are in a random order. Note: this parameter was introduced with WordPress:Version 2.5.
exclude
(string) 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) 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.

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

Related

模板:Tag Tag Tags

模板:PHP Function Tag Footer

This page is [[WordPress::Category:Stubs|marked]] as incomplete. You can help Codex by expanding it.