PHP/Wordpress:类别小部件输出


PHP / Wordpress : category widget output

有没有一种方法可以在不修改wordpress核心文件的情况下修改wordpress-classification/archives小部件html输出?例如,我有这样的输出代码:

<aside><h4>Title</h4><ul><li class="cat-item cat-item-11"><a href="#">123</a> (1)</li></ul></aside>

我正在努力实现的目标:

<aside><h4>Title</h4><ul><li class="cat-item cat-item-11"><a href="#">123</a> <span class="number">[1]<span></li></ul></aside>

您有过滤器widget_categories_dropdown_argswidget_categories_args来更改Categories小部件的输出
示例:https://wordpress.stackexchange.com/a/25002/12615

对于Archives小部件,widget_archives_dropdown_argswidget_archives_args
示例:https://wordpress.stackexchange.com/q/19945/12615

/*
 * Add spans around category counters 
*/
function _thz_filter_wp_list_categories($output, $args) {
    if($args['show_count']){
        $re = '/<'/a>(.*?)<'/li>/s';
        preg_match_all($re, $output, $matches);
        if(!empty($matches) && isset($matches[1])){
            foreach($matches[1] as $between){
                $between    = strip_tags($between);
                $output     = str_replace($between,'<span class="count">'.$between.'</span>',$output);
            }
        }
    }
    return $output;
}
add_filter( 'wp_list_categories', '_thz_filter_wp_list_categories',10,2 );
/*
 * Add spans around archive counters 
*/
function _thz_filter_get_archives_link( $output ) { 
    $re = '/<'/a>(.*?)<'/li>/s';
    preg_match_all($re, $output, $matches);
    if(!empty($matches) && isset($matches[1])){
        foreach($matches[1] as $between){
            $between = strip_tags($between);
            $output  = str_replace($between,'<span class="count">'.$between.'</span>',$output);
        }
    }
    return $output; 
}; 
add_filter( 'get_archives_link', '_thz_filter_get_archives_link', 10, 6 );