Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

It looks like adding current-cat-ancestor class to category list was proposed, but never implemented. The only way I've been able to find so far is to re-create the wp_list_categories() function entirely:

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

is there a better way around this? Thanks!

share|improve this question
2  
I don't think there is. Yours and Tom's answers are very similar in approach. The only alternative I can think of is to use a custom walker class - due to lack of filters that means reproducing the entire start_el - method and walk methods. I'm not sure that's much better... –  Stephen Harris Oct 1 '12 at 13:27
    
Hey @StephenHarris appreciate the insight on this. –  Zach Oct 1 '12 at 13:41
add comment

2 Answers 2

up vote 2 down vote accepted

After fumbling around with this for a bit, I realized I needed this to work with custom taxonomies, not just regular categories. I unfortunately couldn't use "all" of what either the solution I found or Tom did, so I ended up writing my own. As long as you specify a taxonomy arg, you should be all set:

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

Really hoping this gets into WordPress at some point though. Thanks!

share|improve this answer
add comment

I don't see a simple way of hooking in, there may be if you dig deeper, and it's probably better to use a nav menu if you're dealing with a list that requires that kind of heirarchical navigation

If you're intersted in alternatives, I found a solution here:

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

Here's a slightly modified version:

$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;
}
share|improve this answer
    
I really appreciate the response. My needs changed a little (answered above), but hopefully this will allow others to plug into what I've done above for their own needs as well. Thanks! –  Zach Oct 1 '12 at 15:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.