这是标记云小部件元素(wp4.8)的示例输出:
<a href="http://localhost/wp-beta-test/tag/handcrafted/" class="tag-cloud-link tag-link-327 tag-link-position-22" style="font-size: 8pt;" aria-label="handcrafted (2 items)">handcrafted<span class="tag-link-count"> (2)</span></a>
如您所见,小部件还使用括号输出一些aria标签,这会干扰简单的字符串替换。包含括号的标记号也已包装在一个范围内。
例如,尝试使用:
function tagcloud_postcount_filter ($variable) {
$variable = str_replace(\'<span class="tag-link-count"> (\', \' <span class="post_count"> \', $variable);
$variable = str_replace(\')</span>\', \'</span>\', $variable);
return $variable;
}
add_filter(\'wp_tag_cloud\',\'tagcloud_postcount_filter\');