CURRENT_CAT_ANCESTOR备选方案

时间:2012-09-28 作者:Zach

看起来像是adding current-cat-ancestor class to category list 已提出,但从未实施。到目前为止,我找到的唯一方法是重新创建wp_list_categories() 全部功能:

function wp_list_categories2($args = \'\') {
    global $cat;
    if($args == \'\')
        $wp_list_categories = wp_list_categories();
    else
        $wp_list_categories = wp_list_categories($args);

    $cat_id_cut1 = explode(\'cat-item-\', $wp_list_categories);

    for($i=1; $i<sizeof($cat_id_cut1); $i++) {
        $cat_id_cut2 = explode(\'"><a\', $cat_id_cut1[$i]);
        $category_id_array[] = $cat_id_cut2[0];
    }

    for($i=0; $i<sizeof($category_id_array); $i++) {
        if(is_numeric($category_id_array[$i])) {
            if(cat_is_ancestor_of( $category_id_array[$i], $cat)) {
                $wp_list_categories = str_replace($category_id_array[$i], $category_id_array[$i] . \' current-cat-ancestor\', $wp_list_categories);
            }
        }
    }
    return $wp_list_categories;
}
有没有更好的办法?谢谢

2 个回复
最合适的回答,由SO网友:Zach 整理而成

在摸索了一会儿之后,我意识到我需要这个来处理自定义分类法,而不仅仅是常规类别。不幸的是,我无法“全部”使用我找到的解决方案或Tom所做的,所以我最终写下了自己的解决方案。只要指定taxonomy arg,您应该已全部设置好:

function add_category_ancestor_class($args) {
    $list_args = $args;
    $list_args[\'echo\'] = \'0\';
    $catlist = wp_list_categories($list_args);
    if ( is_tax($list_args[\'taxonomy\']) ) {
        global $wp_query;
        $term = $wp_query->get_queried_object();
        $term_object = get_term_by(\'id\', $term->term_id, $list_args[\'taxonomy\']);

        $current_term = $term->term_id;

        $ancestors = get_ancestors($current_term, $list_args[\'taxonomy\']);

        // how many levels more than two set hierarchical ancestor?
        // count from 1 array from 0 : 1:0=Current 2:1=Parent >2:1 all Ancestors
        if( count($ancestors) >= 2){
            $max = count($ancestors) - 1; //Array elements zero based = count - 1
            $extra_class=\'current-cat-ancestor\';
            for ( $counter = 1; $counter <= $max; $counter ++) {
                $cat_ancestor_class = \'cat-item cat-item-\'. $ancestors[$counter];
                $amended_class = $cat_ancestor_class . \' \' . $extra_class;
                $catlist = str_replace($cat_ancestor_class, $amended_class, $catlist );
            }
        }
    }
    $menu = str_replace( array( "\\r", "\\n", "\\t" ), \'\', $catlist );
    return $menu;
}
真的希望这能在某个时候进入WordPress。谢谢

SO网友:Tom J Nowell

我看不到一个简单的方法,如果你深入挖掘,可能会有,如果你处理的列表需要这种继承人的导航,使用导航菜单可能更好

如果您对备选方案感兴趣,我在这里找到了一个解决方案:

http://brassblogs.com/code-snippets/add-current-category-ancestor-to-wp_list_categories

这里有一个稍微修改过的版本:

$args = array(
    \'sort_column\' =>\'name\',
    \'sort_order\' => \'asc\',
    \'style\' =>\'list\',
    \'depth\' =>\'0\',
    \'hierarchical\' =>\'true\',
    \'title_li\' =>\'0\',
    \'hide_empty\' =>\'1\',
);
echo add_category_ancestor_class($args);

function add_category_ancestor_class($args) {    
    $list_args = $args;
    $list_args[\'echo\'] = \'0\';
    $catlist = wp_list_categories($list_args);
    if ( is_category() ) {
        //Get the active category id
        global $cat;
        $curr_cat = get_category($cat);
        $catid = $curr_cat->cat_ID;
        //Find the top level id and number of levels    
        while ($catid){  
            $curr_cat = get_category($catid); 
            $catid = $curr_cat->category_parent; 
            $id[] = $curr_cat->cat_ID;          
        }
        //How many levels more than two set hierarchical ancestor?
        //Count from 1 array from 0 : 1:0=Current 2:1=Parent >2:1 all Ancestors
        if( count($id) > 2){
            $max=count($id)-1; //Array elements zero based = count - 1
            $extra_class=\'current-cat-ancestor\';
            for ( $counter = 1; $counter <= $max; $counter ++) {
                $cat_ancestor_class = \'cat-item cat-item-\'. $id[$counter];
                $amended_class = $cat_ancestor_class . \' \' . $extra_class;
                $catlist = str_replace($cat_ancestor_class, $amended_class, $catlist );
            }
        }
    }
    $menu = str_replace( array( "\\r", "\\n", "\\t" ), \'\', $catlist );
    return $menu;
}

结束