In `wp_list_categories()`, 'current_category' should accept an array of values.

This allows the 'current-cat' or 'current-cat-parent' classes to be applied
to more than one item in the list.

Props vilkatis.
Fixes #33565.

git-svn-id: https://develop.svn.wordpress.org/trunk@33804 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-08-29 19:45:35 +00:00
parent 5b0c402e6f
commit 27c8977af9
2 changed files with 33 additions and 7 deletions

View File

@ -461,7 +461,8 @@ function wp_dropdown_categories( $args = '' ) {
* Display or retrieve the HTML list of categories. * Display or retrieve the HTML list of categories.
* *
* @since 2.1.0 * @since 2.1.0
* @since 4.4.0 Introduced the `hide_title_if_empty` argument. * @since 4.4.0 Introduced the `hide_title_if_empty` argument. The `current_category` argument was modified to
* optionally accept an array of values.
* *
* @param string|array $args { * @param string|array $args {
* Array of optional arguments. * Array of optional arguments.
@ -488,7 +489,8 @@ function wp_dropdown_categories( $args = '' ) {
* @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along * @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along
* with their descendants. See {@link get_terms()}. Default empty string. * with their descendants. See {@link get_terms()}. Default empty string.
* @type bool|int $echo True to echo markup, false to return it. Default 1. * @type bool|int $echo True to echo markup, false to return it. Default 1.
* @type int $current_category Category that should get the 'current-cat' class. Default 0. * @type int|array $current_category ID of category, or array of IDs of categories, that should get the
* 'current-cat' class. Default 0.
* @type bool $hierarchical Whether to include terms that have non-empty descendants. * @type bool $hierarchical Whether to include terms that have non-empty descendants.
* See {@link get_terms()}. Default true. * See {@link get_terms()}. Default true.
* @type string $title_li Text to use for the list title `<li>` element. Pass an empty string * @type string $title_li Text to use for the list title `<li>` element. Pass an empty string
@ -1121,11 +1123,18 @@ class Walker_Category extends Walker {
); );
if ( ! empty( $args['current_category'] ) ) { if ( ! empty( $args['current_category'] ) ) {
$_current_category = get_term( $args['current_category'], $category->taxonomy ); // 'current_category' can be an array, so we use `get_terms()`.
if ( $category->term_id == $args['current_category'] ) { $_current_terms = get_terms( $category->taxonomy, array(
$css_classes[] = 'current-cat'; 'include' => $args['current_category'],
} elseif ( $category->term_id == $_current_category->parent ) { 'hide_empty' => false,
$css_classes[] = 'current-cat-parent'; ) );
foreach ( $_current_terms as $_current_term ) {
if ( $category->term_id == $_current_term->term_id ) {
$css_classes[] = 'current-cat';
} elseif ( $category->term_id == $_current_term->parent ) {
$css_classes[] = 'current-cat-parent';
}
} }
} }

View File

@ -45,6 +45,23 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat-parent[^"]*"/', $found ); $this->assertNotRegExp( '/class="[^"]*cat-item-' . $c2 . '[^"]*current-cat-parent[^"]*"/', $found );
} }
/**
* @ticket 33565
*/
public function test_current_category_should_accept_an_array_of_ids() {
$cats = $this->factory->category->create_many( 3 );
$found = wp_list_categories( array(
'echo' => false,
'hide_empty' => false,
'current_category' => array( $cats[0], $cats[2] ),
) );
$this->assertRegExp( '/class="[^"]*cat-item-' . $cats[0] . '[^"]*current-cat[^"]*"/', $found );
$this->assertNotRegExp( '/class="[^"]*cat-item-' . $cats[1] . '[^"]*current[^"]*"/', $found );
$this->assertRegExp( '/class="[^"]*cat-item-' . $cats[2] . '[^"]*current-cat[^"]*"/', $found );
}
/** /**
* @ticket 16792 * @ticket 16792
*/ */