请尝试以下代码:
add_filter ( \'wp_tag_cloud\', \'tag_cloud_font_size_class\' );
function tag_cloud_font_size_class( $taglinks ) {
$tags = explode(\'</a>\', $taglinks);
$regex1 = "#(.*style=\'font-size:)(.*)((pt|px|em|pc|%);\'.*)#e";
$regex2 = "#(style=\'font-size:)(.*)((pt|px|em|pc|%);\')#e";
$regex3 = "#(.*class=\')(.*)(\' title.*)#e";
foreach( $tags as $tag ) {
$size = preg_replace($regex1, "(\'\'.round($2).\'\')", $tag ); //get the rounded font size
$tag = preg_replace($regex2, "(\'\')", $tag ); //remove the inline font-size style
$tag = preg_replace($regex3, "(\'$1tag-size-\'.($size).\' $2$3\')", $tag ); //add .tag-size-{nr} class
$tagn[] = $tag;
}
$taglinks = implode(\'</a>\', $tagn);
return $taglinks;
}
可能有更有效的解决方案,或者有一种方法可以将其全部放在一个正则表达式中;但它是有效的(我不是preg\\u replace()方面的专家)。可与同时使用
slug class 滤器