PHP / Wordpress:类别小部件输出

时间:2013-08-28 19:03:00

标签: php wordpress widget

有没有办法修改wordpress类别/档案小部件html输出而无需修改wordpress核心文件?例如,我有这个输出代码:

<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>

2 个答案:

答案 0 :(得分:0)

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

对于档案窗口小部件,widget_archives_dropdown_argswidget_archives_args 示例:https://wordpress.stackexchange.com/q/19945/12615

答案 1 :(得分: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 );