将跨度添加到存档 post 计数

Add span to archive post count

在 WordPress 中,您可以向 wp_list_categories() 函数添加一个跨度,例如:

function style_the_list_count($links) {
$links = str_replace('</a> (', '</a> <span class="listCount">(', $links);
$links = str_replace(')', ')</span>', $links);
return $links;
}
add_filter('wp_list_categories', 'style_the_list_count');

但我想以存档的 Show post counts 为目标,但在寻找要绑定的功能后,我一直无法找到应该使用的功能。我已经尝试 wp_get_archives 从我的搜索但没有运气,当我在 post Creating an Archive Index 下查看时,我没有看到任何提及。有没有一种方法可以连接到存档计数,或者有一种方法可以为所有默认小部件的已检查 Show post counts 小部件的每个实例添加 span 标签?

wp_get_archives() 本身没有任何我们可以挂钩的有用过滤器,但是 get_archives_link()(它调用并将 post 计数输出传递给)有。

您可以使用几乎相同的函数并将其挂接到 get_archives_link 过滤器:

function so_40551791_style_the_archive_count($links) {
    $links = str_replace('</a>&nbsp;(', '</a> <span class="archiveCount">(', $links);
    $links = str_replace(')', ')</span>', $links);
    return $links;
}

add_filter('get_archives_link', 'so_40551791_style_the_archive_count');

注意,&nbsp;之前有一个space。