论点topic_count_text_callback
无法执行您需要的操作,因为它没有将术语ID和分类法作为参数。这应该固定在核心 我想我以后会为此写一个补丁
更新:我已经为Ticket #21198. 里程碑是3.6,因此以下答案最终将过时。我会更新这个帖子
但并非所有的希望都破灭了。我们可以在上筛选生成的标记wp_tag_cloud
, 对其运行regex并从class
属性和第二个参数的分类$args
. 然后,我们使用术语ID和分类法来获取术语描述并替换原始标题属性。
add_filter(
\'wp_tag_cloud\', # filter name
array ( \'WPSE_78426_Tag_Cloud_Filter\', \'filter_cloud\' ), # callback
10, # priority
2 # number of arguments
);
/**
* Replace title attribut in a tag cloud with term description
*
* @author toscho http://toscho.de
*/
class WPSE_78426_Tag_Cloud_Filter
{
/**
* Current taxonomy
*
* @type string
*/
protected static $taxonomy = \'post_tag\';
/**
* Register current taxonomy and catch term id per regex.
*
* @wp-hook wp_tag_cloud
* @uses preg_callback()
* @param string $tagcloud Tab cloud markup
* @param array $args Original arguments for wp_tag_cloud() call
* @return string Changed markup
*/
public static function filter_cloud( $tagcloud, $args )
{
// store the taxonomy for later use in our callback
self::$taxonomy = $args[\'taxonomy\'];
return preg_replace_callback(
\'~class=\\\'tag-link-(\\d+)\\\' title=\\\'([^\\\']+)\\\'~m\',
array ( __CLASS__, \'preg_callback\' ),
$tagcloud
);
}
/**
* Replace content of title attribute.
*
* @param array $matches
* $matches[0] = complete matched string,
* $matches[1] = term id,
* $matches[2] = original content of title attribute
* @return string
*/
protected static function preg_callback( $matches )
{
$term_id = $matches[1];
// get term description
$desc = term_description( $term_id, self::$taxonomy );
// remove HTML
$desc = wp_strip_all_tags( $desc, TRUE );
// escape unsafe chacters
$desc = esc_attr( $desc );
// rebuild the attributes, keep delimiters (\') intact
// for other filters
return "class=\'tag-link-$term_id\' title=\'$desc\'";
}
}
结果