experience

经验

zw-1988.com

home > 经验 > wordpress 标签云函数

2017.8.11 wordpress 标签云函数

zwnet

1.直接用

<?php wp_tag_cloud(”); ?>

设置的参数

<?php wp_tag_cloud(‘number=40&orderby=count&order=DESC&smallest=8&largest=15&unit=px’); ?>

各参数解释:
number表示显示多少个标签,比如40个
orderby表示调用标签的排序,默认为“name”按名称排序,“count”则按关联的文章数量排列;
order表示排序的方式DESC表示倒序,即数量越多的在前面,通常采用倒序,因为tag文章量越多,说明tag也重要,用户浏览的几率就越大,ASC表示正序,RAND表示随机
smallest表示标签文字字号的大小,比如最小是8
largest表示标签文字字号的大小,比如最大是15
unit表示标签文字字号的单位,可以为px、em、pt、百分比等,通常是用PX
还有两个可以设置的参数
exclude表示排除某些标签
include表示只包含某些标签

默认参数在wp-includes/category-template.php 如下

function wp_tag_cloud( $args = '' ) {
	$defaults = array(
		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
		'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
		'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true,
		'show_count' => 0,
	);
	$args = wp_parse_args( $args, $defaults );

	$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags

	if ( empty( $tags ) || is_wp_error( $tags ) )
		return;

	foreach ( $tags as $key => $tag ) {
		if ( 'edit' == $args['link'] )
			$link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
		else
			$link = get_term_link( intval($tag->term_id), $tag->taxonomy );
		if ( is_wp_error( $link ) )
			return;

		$tags[ $key ]->link = $link;
		$tags[ $key ]->id = $tag->term_id;
	}

	$return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args

	/**
	 * Filters the tag cloud output.
	 *
	 * @since 2.3.0
	 *
	 * @param string $return HTML output of the tag cloud.
	 * @param array  $args   An array of tag cloud arguments.
	 */
	$return = apply_filters( 'wp_tag_cloud', $return, $args );

	if ( 'array' == $args['format'] || empty($args['echo']) )
		return $return;

	echo $return;
}

2彩色标签

在需要用的页面位置写入

<?php $html = '<div class="tagcloud">';
foreach (get_tags( array('number' => 50, 'orderby' => 'count', 'order' => 'DESC', 'hide_empty' => false) ) as $tag){
	$color = dechex(rand(0,16777215));
	$tag_link = get_tag_link($tag->term_id);
 
	$html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}' style='color:#{$color}'>";
	$html .= "{$tag->name} ({$tag->count})</a>";
}
$html .= '</div>';
echo $html;?>

3彩色背景框

在2的基础上把

$color = dechex(rand(0,16777215));

替换成

$colors = array('ffeded','f9e5ff','ffe4d7','d9ffff','dbfdec','d7edff','dcf3c2','fdf4d5');  
$color=$colors[dechex(rand(0,7))];

 

«
相关文章
wordpress (25)java script (9)tool (8)css (2)攻略 (1)第一次人体 (1)插件 (1)ECharts (1)food (1)雪糕 (1)动画 (1)book (1)标签云 (1)生活 (1)一言难尽的画稿 (1)
辽ICP备16002031号

Copyright © 2016 - 2020 1988-zw.com