1

有没有办法在不修改 wordpress 核心文件的情况下修改 wordpress 类别/档案小部件 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>
4

2 回答 2

0

您拥有过滤器widget_categories_dropdown_argswidget_categories_args可以更改类别小部件的输出。
示例:https ://wordpress.stackexchange.com/a/25002/12615

对于档案小部件,widget_archives_dropdown_args以及widget_archives_args.
示例:https ://wordpress.stackexchange.com/q/19945/12615

于 2013-08-29T19:54:04.000 回答
0
/*
 * 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 ); 
于 2017-02-07T16:23:38.403 回答