Add `current-cat-ancestor` class to ancestor items in `wp_list_categories()`.

Pairs nicely with `current-cat-parent`.

Props jrchamp, swisssipdy, ardathksheyna, wonderboymusic.
Fixes #10676.

git-svn-id: https://develop.svn.wordpress.org/trunk@36008 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-12-18 18:37:41 +00:00
parent 3969f2968f
commit aa38888d60
2 changed files with 28 additions and 0 deletions

View File

@ -170,6 +170,13 @@ class Walker_Category extends Walker {
} elseif ( $category->term_id == $_current_term->parent ) {
$css_classes[] = 'current-cat-parent';
}
while ( $_current_term->parent ) {
if ( $category->term_id == $_current_term->parent ) {
$css_classes[] = 'current-cat-ancestor';
break;
}
$_current_term = get_term( $_current_term->parent, $category->taxonomy );
}
}
}

View File

@ -381,4 +381,25 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
$this->assertNotContains( '<li class="cat-item cat-item-' . $child3 . '">', $actual );
$this->assertNotContains( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
}
/**
* @ticket 10676
*/
public function test_class_containing_current_cat_ancestor() {
$parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
$child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
$child2 = self::factory()->category->create( array( 'name' => 'Child 2', 'slug' => 'child2', 'parent' => $parent ) );
$grandchild = self::factory()->category->create( array( 'name' => 'Grand Child', 'slug' => 'child', 'parent' => $child ) );
$actual = wp_list_categories( array(
'echo' => 0,
'hide_empty' => false,
'current_category' => $grandchild,
) );
$this->assertRegExp( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertRegExp( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $grandchild . '[^"]*current-cat-ancestor[^"]*"/', $actual );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $child2 . '[^"]*current-cat-ancestor[^"]*"/', $actual );
}
}