Login dark
author: LingAo
title: Wordpress模板标签wp_tag_cloud()介绍
date: 2011-08-31 09:40:04
category: [编程代码]
tags: [Wordpress,主题,模板]

<span class="Apple-style-span"><span class="Apple-style-span">

说明

<p style="margin-top: 0.4em;margin-bottom: 0.8em">        该标签于WordPress 2.3版本投入使用,可在所谓的“标签云”中显示多个标签,所显示标签的大小取决于该标签的使用频率。WordPress 2.8中的wp_tag_cloud引入了taxonomy参数,使分类法被用作云的产生基础。这意味着,例如,你可以通过wp_tag_cloud向访问者展示文章的分类云。</p> <!--more-->

用法

<p style="margin-top: 0.4em;margin-bottom: 0.8em"><?php wp_tag_cloud( $args ); ?&gt;</p> <p style="margin-top: 0.4em;margin-bottom: 0.8em"><strong>默认用法</strong></p><pre> &lt;?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 ); ?></pre>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">默认情况下所显示的结果:</p>

  • smallest<span class="Apple-converted-space"> </span>—— 最小的标签(使用次数最少)显示大小为8
  • largest<span class="Apple-converted-space"> </span>——最大的标签(使用次数最多)显示大小为22
  • unit<span class="Apple-converted-space"> </span>—— 最大值最小值的单位为'pt'
  • number<span class="Apple-converted-space"> </span>—— 至多显示45个标签
  • format<span class="Apple-converted-space"> </span>—— 以平面形式显示所有标签(标签之间用空格隔开)
  • separator<span class="Apple-converted-space"> </span>—— 显示标签之间的空格
  • orderby<span class="Apple-converted-space"> </span>—— 按名称为标签排序
  • order<span class="Apple-converted-space"> </span>—— 以升序排列
  • exclude<span class="Apple-converted-space"> </span>—— 不排除任何标签
  • include<span class="Apple-converted-space"> </span>—— 包括所有标签
  • link<span class="Apple-converted-space"> </span>—— 可视
  • taxonomy<span class="Apple-converted-space"> </span>—— 用文章的标签作为云基础
  • echo<span class="Apple-converted-space"> </span>—— 响应结果

参数


<p style="margin-top: 0.4em;margin-bottom: 0.8em">smallest</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(整数)(可选)使用次数最少的标签的字号大小(单位由unit参数决定)</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">默认值:8</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">largest</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(整数)(可选)使用次数最多的标签的字号大小(单位由unit参数决定)</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">默认值:22</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">unit</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)对smallestlargest的值的测量单位。可以是任何CSS长度单位,如pt, px, em, %。</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">默认值:'pt'</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">number</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(整数)(可选)显示在云中的实际标签数。(值为'0'时显示所有标签)</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">默认值:45</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">format</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)所显示的云的格式。</p>

  • 'flat' (默认值)标签被“separator”参数所定义的空格分隔
  • 'list' 标签与class='wp-tag-cloud' 共同在UL中
  • 'array' 标签在数组中,函数以数组方式返回标签云,以用在PHP中。注意:数组被返回,而非显示,引入于WordPress2.5

<p style="margin-top: 0.4em;margin-bottom: 0.8em">separator</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)标签之间的文本/空格。注意:该参数引入于WordPress<span class="Apple-converted-space"> </span>2.9。</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">默认值:'/n' (空格)</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">orderby</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)标签的排列顺序。有效值包括:</p>

  • 'name' (默认值)
  • 'count'

<p style="margin-top: 0.4em;margin-bottom: 0.8em">order</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)排列顺序(升序或降序)。有效值包括——必须大写:</p>

  • 'ASC' ——默认值
  • 'DESC'
  • 'RAND' —— 以任意顺序排列标签。注意:该标签引入于WordPress<span class="Apple-converted-space"> </span>2.5

<p style="margin-top: 0.4em;margin-bottom: 0.8em">exclude</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)将要被排除的标签(term_id)的ID,各ID用逗号隔开。如 'exclude=5,27'表示不显示term_id为5或27的标签。默认值为不排除任何标签。</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">include</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)要包含的标签(term_id)列表,各ID用逗号隔开。例如, 'include=5,27' 表示只显示term_id为5或27的标签。默认为包含所有链接。</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">link</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)设置链接,允许编辑某个指定标签。注意:该标签引入于WordPress<span class="Apple-converted-space"> </span>2.7。有效值包括:</p>

  • 'view' (默认值)
  • 'edit'

<p style="margin-top: 0.4em;margin-bottom: 0.8em">taxonomy</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(字符串)(可选)用以生成云的分类法。注意:该标签引入于WordPress2.8。</p>

  • 'post_tag' —— (默认值)将文章标签当作云的来源
  • 'category' —— 用文章分类生成云
  • 'link_category' —— 用链接分类目录生成云

<p style="margin-top: 0.4em;margin-bottom: 0.8em">echo</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">(布尔型)(可选)显示结果,或将结果保留在变量中。默认值为true(显示标签云)。注意:该标签引入于WordPress2.8。有效值包括:</p>

  • 1 (true) —— 默认值
  • 0 (false)

示例


<p style="margin-top: 0.4em;margin-bottom: 0.8em">显示常用标签标题的云</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em"><?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; ?></p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">限制标签大小且以使用次数而非名称排列标签的云</p><pre><?php wp_tag_cloud('smallest=8&largest=22&number=30&orderby=count'); ?> </pre>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">以数组形式返回云,但不显示</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">在变量$tag中包含标签云,以用在其它PHP代码中</p><pre> &lt;?php $tag = wp_tag_cloud('format=array' );?> </pre>

创建标签存档


<p style="margin-top: 0.4em;margin-bottom: 0.8em">        WordPress 2.3新增了一个显著功能——标签,wp_tag_cloud标签可用来展示一个标签存档索引。这就表示,访问者在显示标签云的页面上点击某个标签时,该标签所属的所有文章都会展示在页面上。根据模板层级,如果tag.php模板不存在,那么就使用archives.php模板。通过tag,php模板你可以自定义标签存档索引的样式,为方便导航,模板会在最上方包含标签云。</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">要将标签云显示在模板上方,你需要将一个新模板添加到主题文件中。模板模板层级中有相关介绍。基础步骤包括:</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">1. 用下面的内容创建一个文件,命名为tag.php</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">2. 将新文件上传到主题目录下</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">3. 如果你希望在页面导航中加入一个指向标签索引的链接,可进行第三步骤,否则点击某个标签时会使用新模板。</p>

  • 用新模板新建一个空白页面,将页面命名为标签存档索引。

<p style="margin-top: 0.4em;margin-bottom: 0.8em">对第三步的进一步阐述:</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">        WordPress可为不同页面使用不同页面模板。在页面>添加新页面界面的最下方(或是侧边栏,取决于你安装的WordPress版本)有一个名为“页面模板”的下拉式菜单。你可以在这里选择显示某个页面所用的模板。</p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em"><?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></p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em"><?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?></p>
<p style="margin-top: 0.4em;margin-bottom: 0.8em">        注意,模板还没有添加样式。通过查看single.php主题文件可以了解你的主题所用的结构。</p></span></span>