Wordpress 单桩上的当前cat类

Wordpress 单桩上的当前cat类,wordpress,taxonomy,wp-list-categories,Wordpress,Taxonomy,Wp List Categories,我正在尝试使用默认的WordPress categories小部件在单个帖子上添加当前的cat类,就像在category archive页面上一样 我尝试从functions.php的答案中添加以下代码: add_filter( 'wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2 ); function sgr_show_current_cat_on_single( $output, $args ) { if ( i

我正在尝试使用默认的WordPress categories小部件在单个帖子上添加当前的cat类,就像在category archive页面上一样

我尝试从functions.php的答案中添加以下代码:

add_filter( 'wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2 );
function sgr_show_current_cat_on_single( $output, $args ) {
    if ( is_single() ) :
        global $post;
        $terms = get_the_terms( $post->ID, $args['taxonomy'] );
        foreach( $terms as $term ) {
            if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) ) {
                $output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
            }
        }
    endif;
    return $output;
}
但这会导致以下错误:

警告:第6行的functions.php中为foreach()提供的参数无效


您的参数错误此筛选器中只使用了一个参数,因此您的函数应该如下所示

   function sgr_show_current_cat_on_single($args) 

更多信息请查看此处

啊,谢谢!现在返回以下错误:警告:第5行functions.php中的非法字符串偏移量“taxonomy”