wordpress4.4以上版本标签云自定义参数输出方法

wordpress 标签云支持不少自定义参数,参考:WordPress函数 wp_tag_cloud() 标签云

wordpress 4.3.x 及以前的版本中,wp_tag_cloud函数将参数直接 'echo' => true 输出,可使用如下代码来自定义标签云的参数:

add_filter('widget_tag_cloud_args','tag_cloud_style');
function tag_cloud_style($args){
	$args = array(
		'smallest'	=> 12,
		'largest'	=> 18,
		'unit'		=> 'px',
		'number'	=> 50,
		'orderby'	=> 'count',
		'order'		=> 'DESC',
	);
	return $args;
}

wordpress 4.4 及以后版本中,由于 wp_tag_cloud 函数修改了设置 'echo' => false ,因此需要将自定义属性变量合并为一个数组,代码如下:

add_filter('widget_tag_cloud_args','tag_cloud_style',10);
function tag_cloud_style($args){
	$diy_args = array(
		'smallest'	=> 12,
		'largest'	=> 18,
		'unit'		=> 'px',
		'number'	=> 50,
		'orderby'	=> 'count',
		'order'		=> 'DESC',
	);
	$args = wp_parse_args( $args, $diy_args );
	return $args;
}

附上 widget_tag_cloud_args 钩子源代码:

/**
 * Filter the taxonomy used in the Tag Cloud widget.
 *
 * @since 2.8.0
 * @since 3.0.0 Added taxonomy drop-down.
 *
 * @see wp_tag_cloud()
 *
 * @param array $current_taxonomy The taxonomy to use in the tag cloud. Default 'tags'.
 */
$tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
	'taxonomy' => $current_taxonomy,
	'echo' => false
) ) );