wp_list_categories() WITH current_cat_ancestor

2010-01-16 @ 14:50

WordPress have a ”bug”. It doesn’t add a current_cat_ancestor class to the wp_list_pages function. If you need it, you can use the code below.

function wp_list_categories2($args = '')
global $cat;
if($args == '')
$wp_list_categories = wp_list_categories();
$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(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;
RSS-feed for comments

8 replys to “wp_list_categories() WITH current_cat_ancestor”

  • adam
    2010-05-17 @ 1:50 f m

    hi – where do you put this script? what file? thx

  • Phil
    2010-05-17 @ 3:06 f m

    Jens – thanks for this post! saved me some time.

    the only note is that your less than and angle brackets are escaped in the code above.

    eg: $i<sizeof($cat_id_cut1)

    • Phil
      2010-05-17 @ 3:07 f m

      ok – hilarious — i should have escaped the escaped code example, but you get my point.

  • Ben
    2010-06-01 @ 7:50 e m

    maybe you can help with this: how do I get a parent-cat class added in to all categories that have children in the normal wp_list_categories. This above code works well when on a child page, but can you help with how to simply add the parent-cat class? Trying to do it as a filter but have VERY limited php knowledge. Thanks!

  • Mike
    2010-07-01 @ 4:03 e m

    I put the code in my functions.php file, then got this error: syntax error, unexpected ‘;’, expecting ‘)’

    The error is in the line Phil is referring to above.

    I changed the ”less than” HTML entity to ”<", and the error went away, but the function did nothing.

    Any suggestions?

    Thanks in advance for any help. This is an essential function!

  • chai »
    2010-10-24 @ 1:40 e m

    Can modify with css tab navi manu?

  • matt mcinvale »
    2012-01-04 @ 12:54 f m

    much thanks for sharing this code, worked great with a 3.3.1 install i’m working on.

Leave a reply