Make WordPress Core

Changeset 36008


Ignore:
Timestamp:
12/18/2015 06:37:41 PM (8 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-walker-category.php

    r35140 r36008  
    171171                        $css_classes[] = 'current-cat-parent';
    172172                    }
     173                    while ( $_current_term->parent ) {
     174                        if ( $category->term_id == $_current_term->parent ) {
     175                            $css_classes[] =  'current-cat-ancestor';
     176                            break;
     177                        }
     178                        $_current_term = get_term( $_current_term->parent, $category->taxonomy );
     179                    }
    173180                }
    174181            }
  • trunk/tests/phpunit/tests/category/wpListCategories.php

    r36005 r36008  
    382382        $this->assertNotContains( '<li class="cat-item cat-item-' . $child4 . '">', $actual );
    383383    }
     384
     385    /**
     386     * @ticket 10676
     387     */
     388    public function test_class_containing_current_cat_ancestor() {
     389        $parent = self::factory()->category->create( array( 'name' => 'Parent', 'slug' => 'parent' ) );
     390        $child = self::factory()->category->create( array( 'name' => 'Child', 'slug' => 'child', 'parent' => $parent ) );
     391        $child2 = self::factory()->category->create( array( 'name' => 'Child 2', 'slug' => 'child2', 'parent' => $parent ) );
     392        $grandchild = self::factory()->category->create( array( 'name' => 'Grand Child', 'slug' => 'child', 'parent' => $child ) );
     393
     394        $actual = wp_list_categories( array(
     395            'echo'             => 0,
     396            'hide_empty'       => false,
     397            'current_category' => $grandchild,
     398        ) );
     399
     400        $this->assertRegExp( '/class="[^"]*cat-item-' . $parent . '[^"]*current-cat-ancestor[^"]*"/', $actual );
     401        $this->assertRegExp( '/class="[^"]*cat-item-' . $child . '[^"]*current-cat-ancestor[^"]*"/', $actual );
     402        $this->assertNotRegExp( '/class="[^"]*cat-item-' . $grandchild . '[^"]*current-cat-ancestor[^"]*"/', $actual );
     403        $this->assertNotRegExp( '/class="[^"]*cat-item-' . $child2 . '[^"]*current-cat-ancestor[^"]*"/', $actual );
     404    }
    384405}
Note: See TracChangeset for help on using the changeset viewer.