我正在寻找一种方法来获取从wp_get_archives
作为数组,类似于get_categories
. 我需要这样做才能修改要使用的post计数器- [#]
而不是(#)
. 请参见下面我的当前代码:
My code for categories, which displays as desired:
$cats = get_categories();
if ($cats) {
echo "<section class=\'widget\'>";
echo "<header><h4>Categories</h4></header>";
echo "<ul>";
foreach ($cats as $cat) {
echo "<li><a href=\'" . get_category_link($cat->term_id) . "\'>" . $cat->name . "</a> - [" . $cat->count . "]</li>";
}
echo "</ul>";
echo "</section><!--/.widget-->";
}
My code for archvies, which does not display as desired:
$archives = wp_get_archives(array(
"echo" => 0,
"show_post_count" => true,
));
if ($archives) {
echo "<section class=\'widget\'>";
echo "<header><h4>Archives</h4></header>";
echo "<ul class=\'cats\'>" . $archives . "</ul>";
echo "</section><!--/.widget-->";
}
最合适的回答,由SO网友:s_ha_dum 整理而成
如果你看wp_get_archives()
你会注意到the link is generated 通过get_archives_link()
. 该函数提供一个过滤器,允许您替换parens。
这相当粗糙,但确实有效。
function archive_link_wpse_183665($link) {
$pat = \'|\\(([^)])\\)</li>|\';
// preg_match($pat,$link,$matches);
// var_dump($matches);
$link = preg_replace($pat,\'[$1]\',$link);
return $link;
}
add_filter( \'get_archives_link\', \'archive_link_wpse_183665\' );