Introduce `hide_title_if_no_cats` parameter to `wp_list_categories()`.
When generating a `<ul>` using `wp_list_categories()`, a title `<li>` element is put at the top of the term list. Current behavior is that this title `<li>` appears even when no terms are found. The new `hide_title_if_no_cats` param allows developers to specify that the title should be hidden when the term list is empty. Props vilkatis. Fixes #33460. git-svn-id: https://develop.svn.wordpress.org/trunk@33764 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
91a24f5cca
commit
a684d5d19d
|
@ -461,6 +461,7 @@ function wp_dropdown_categories( $args = '' ) {
|
|||
* Display or retrieve the HTML list of categories.
|
||||
*
|
||||
* @since 2.1.0
|
||||
* @since 4.4.0 Introduced the `hide_title_if_no_cats` argument.
|
||||
*
|
||||
* @param string|array $args {
|
||||
* Array of optional arguments.
|
||||
|
@ -491,6 +492,8 @@ function wp_dropdown_categories( $args = '' ) {
|
|||
* See {@link get_terms()}. Default true.
|
||||
* @type string $title_li Text to use for the list title `<li>` element. Pass an empty string
|
||||
* to disable. Default 'Categories'.
|
||||
* @type bool $hide_title_if_no_cats Whether to hide the `$title_li` element if there are no terms in
|
||||
* the list. Default false (title will always be shown).
|
||||
* @type int $depth Category depth. Used for tab indentation. Default 0.
|
||||
* @type string $taxonomy Taxonomy name. Default 'category'.
|
||||
* }
|
||||
|
@ -507,6 +510,7 @@ function wp_list_categories( $args = '' ) {
|
|||
'feed_image' => '', 'exclude' => '',
|
||||
'exclude_tree' => '', 'current_category' => 0,
|
||||
'hierarchical' => true, 'title_li' => __( 'Categories' ),
|
||||
'hide_title_if_no_cats' => false,
|
||||
'echo' => 1, 'depth' => 0,
|
||||
'taxonomy' => 'category'
|
||||
);
|
||||
|
@ -534,7 +538,7 @@ function wp_list_categories( $args = '' ) {
|
|||
$categories = get_categories( $r );
|
||||
|
||||
$output = '';
|
||||
if ( $r['title_li'] && 'list' == $r['style'] ) {
|
||||
if ( $r['title_li'] && 'list' == $r['style'] && ( ! empty( $categories ) || ! $r['hide_title_if_no_cats'] ) ) {
|
||||
$output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
|
||||
}
|
||||
if ( empty( $categories ) ) {
|
||||
|
|
|
@ -197,4 +197,54 @@ class Tests_Category_WpListCategories extends WP_UnitTestCase {
|
|||
|
||||
return $cat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 33460
|
||||
*/
|
||||
public function test_title_li_should_be_shown_by_default_for_empty_lists() {
|
||||
$found = wp_list_categories( array(
|
||||
'echo' => false,
|
||||
) );
|
||||
|
||||
$this->assertContains( '<li class="categories">Categories', $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 33460
|
||||
*/
|
||||
public function test_hide_title_if_no_cats_should_be_respected_for_empty_lists_when_true() {
|
||||
$found = wp_list_categories( array(
|
||||
'echo' => false,
|
||||
'hide_title_if_no_cats' => true,
|
||||
) );
|
||||
|
||||
$this->assertNotContains( '<li class="categories">Categories', $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 33460
|
||||
*/
|
||||
public function test_hide_title_if_no_cats_should_be_respected_for_empty_lists_when_false() {
|
||||
$found = wp_list_categories( array(
|
||||
'echo' => false,
|
||||
'hide_title_if_no_cats' => false,
|
||||
) );
|
||||
|
||||
$this->assertContains( '<li class="categories">Categories', $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 33460
|
||||
*/
|
||||
public function test_hide_title_if_no_cats_should_be_ignored_when_category_list_is_not_empty() {
|
||||
$cat = $this->factory->category->create();
|
||||
|
||||
$found = wp_list_categories( array(
|
||||
'echo' => false,
|
||||
'hide_empty' => false,
|
||||
'hide_title_if_no_cats' => true,
|
||||
) );
|
||||
|
||||
$this->assertContains( '<li class="categories">Categories', $found );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue