竹磬网-邵珠庆の日记 生命只有一次,你可以用它来做些更多伟大的事情–Make the world a little better and easier

311月/119

制作标签云页面の模板标签-wp_tag_cloud()

说明

该标签于WordPress 2.3版本投入使用,可在所谓的“标签云”中显示多个标签,所显示标签的大小取决于该标签的使用频率。WordPress 2.8中的wp_tag_cloud引入了taxonomy参数,使分类法被用作云的产生基础。这意味着,例如,你可以通过wp_tag_cloud向访 问者展示文章的分类云。

用法

<?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'   => , 
    'include'   => , 
    'link'      => 'view', 
    'taxonomy'  => 'post_tag', 
    'echo'      => true ); ?>

默认情况下所显示的结果:

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

参数

smallest

(整数)(可选)使用次数最少的标签的字号大小(单位由unit参数决定)

默认值:8

largest

(整数)(可选)使用次数最多的标签的字号大小(单位由unit参数决定)

默认值:22

unit

(字符串)(可选)对smallestlargest的值的测量单位。可以是任何CSS长度单位,如pt, px, em, %。

默认值:'pt'

number

(整数)(可选)显示在云中的实际标签数。(值为'0'时显示所有标签)

默认值:45

format

(字符串)(可选)所显示的云的格式。

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

separator

(字符串)(可选)标签之间的文本/空格。注意:该参数引入于WordPress 2.9

默认值:'/n' (空格)

orderby

(字符串)(可选)标签的排列顺序。有效值包括:

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

order

(字符串)(可选)排列顺序(升序或降序)。有效值包括——必须大写:

  • 'ASC' ——默认值
  • 'DESC'
  • 'RAND' —— 以任意顺序排列标签。注意:该标签引入于WordPress 2.5

exclude

(字符串)(可选)将要被排除的标签(term_id)的ID,各ID用逗号隔开。如 'exclude=5,27'表示不显示term_id为5或27的标签。默认值为不排除任何标签。

include

(字符串)(可选)要包含的标签(term_id)列表,各ID用逗号隔开。例如, 'include=5,27' 表示只显示term_id为5或27的标签。默认为包含所有链接。

link

(字符串)(可选)设置链接,允许编辑某个指定标签。注意:该标签引入于WordPress 2.7。有效值包括:

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

taxonomy

(字符串)(可选)用以生成云的分类法。注意:该标签引入于WordPress2.8

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

echo

(布尔型)(可选)显示结果,或将结果保留在变量中。默认值为true(显示标签云)。注意:该标签引入于WordPress2.8。有效值包括:

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

示例

显示常用标签标题的云

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

限制标签大小且以使用次数而非名称排列标签的云

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

以数组形式返回云,但不显示

在变量$tag中包含标签云,以用在其它PHP代码中

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

创建标签存档

WordPress 2.3新增了一个显著功能——标签,wp_tag_cloud标签可用来展示一个标签存档索引。这就表示,访问者在显示标签云的页面上点击某个标签时,该标签所属的所有文章都会展示在页面上。根据模板层级,如果tag.php模板不存在,那么就使用archives.php模板。通过tag,php模板你可以自定义标签存档索引的样式,为方便导航,模板会在最上方包含标签云。

要将标签云显示在模板上方,你需要将一个新模板添加到主题文件中。模板模板层级中有相关介绍。基础步骤包括:

1. 用下面的内容创建一个文件,命名为tag.php

2. 将新文件上传到主题目录下

3. 如果你希望在页面导航中加入一个指向标签索引的链接,可进行第三步骤,否则点击某个标签时会使用新模板。

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

对第三步的进一步阐述:

WordPress可为不同页面使用不同页面模板。在页面>添加新页面界面的最下方(或是侧边栏,取决于你安装的WordPress版本)有一个名为“页面模板”的下拉式菜单。你可以在这里选择显示某个页面所用的模板。

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

注意,模板还没有添加样式。通过查看single.php主题文件可以了解你的主题所用的结构。

喜欢这个文章吗?

考虑订阅我们的RSS Feed吧!

发布在 邵珠庆

评论 (9) 引用 (0)
  1. I think I’ll go up the street and watch movies.. I’d rather have a friend than be alone right now..

  2. Many thanks for an unbelievable submit, would study your personal others topics. thank you your thinking within this, I soon became a bit strike by this post. Thanks again! You wanna make a good point. Portrays natures best by the wonderful facts here. I do think that in case more individuals consideration for it like this, they’d have a better moment in time obtain the hold ofing the issue.

  3. The DC15 The Ball series currently contains four vacuum models the all floors, animal, total clean, and full kit. If a kitchen has room to accommodate a microwave oven, it should also be able to take one of these.


取消回复

还没有引用.