wp_list_categories更改html输出格式


wp_list_categories change the html output format?

当您在wp_list_categories函数中启用show_count时,默认输出如下所示:

  <li><a href="http://example.com/category/foo/">Foo</a> (559)</li>

其中(559)是类别Foo的总帖子计数。我想要以下格式的输出

  <li><a href="http://example.com/category/foo/">Foo (559)</a></li>

所以我写了以下php代码

  <?php $cat_list = wp_list_categories('sort_column=name&title_li=&use_desc_for_title=0&depth=4&show_count=1&exclude=some_value'); 
                  $cat_list = str_replace('</a>','',$cat_list);
                  $cat_list = str_replace(')', ') </a>', $cat_list);
                  echo $cat_list;
                  ?>

但是,输出不会改变。我得到了相同的输出。知道为什么吗?

您需要使用参数添加&echo=0

试试这个

$cat_list = wp_list_categories('sort_column=name&title_li=&use_desc_for_title=0&depth=4&show_count=1&echo=0'); 
$cat_list = preg_replace('/<'/a> '(([0-9]+)')/', ' (''1)</a>', $cat_list);
echo $cat_list;