WordPress.org

Make WordPress Core

Changeset 33764


Ignore:
Timestamp:
08/26/2015 07:41:55 PM (6 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r33763 r33764  
    462462 *
    463463 * @since 2.1.0
     464 * @since 4.4.0 Introduced the `hide_title_if_no_cats` argument.
    464465 *
    465466 * @param string|array $args {
     
    492493 *     @type string       $title_li           Text to use for the list title `<li>` element. Pass an empty string
    493494 *                                            to disable. Default 'Categories'.
     495 *     @type bool         $hide_title_if_no_cats Whether to hide the `$title_li` element if there are no terms in
     496 *                                               the list. Default false (title will always be shown).
    494497 *     @type int          $depth              Category depth. Used for tab indentation. Default 0.
    495498 *     @type string       $taxonomy           Taxonomy name. Default 'category'.
     
    508511        'exclude_tree' => '', 'current_category' => 0,
    509512        'hierarchical' => true, 'title_li' => __( 'Categories' ),
     513        'hide_title_if_no_cats' => false,
    510514        'echo' => 1, 'depth' => 0,
    511515        'taxonomy' => 'category'
     
    535539
    536540    $output = '';
    537     if ( $r['title_li'] && 'list' == $r['style'] ) {
     541    if ( $r['title_li'] && 'list' == $r['style'] && ( ! empty( $categories ) || ! $r['hide_title_if_no_cats'] ) ) {
    538542        $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
    539543    }
  • trunk/tests/phpunit/tests/category/wpListCategories.php

    r32292 r33764  
    198198        return $cat;
    199199    }
     200
     201    /**
     202     * @ticket 33460
     203     */
     204    public function test_title_li_should_be_shown_by_default_for_empty_lists() {
     205        $found = wp_list_categories( array(
     206            'echo' => false,
     207        ) );
     208
     209        $this->assertContains( '<li class="categories">Categories', $found );
     210    }
     211
     212    /**
     213     * @ticket 33460
     214     */
     215    public function test_hide_title_if_no_cats_should_be_respected_for_empty_lists_when_true() {
     216        $found = wp_list_categories( array(
     217            'echo' => false,
     218            'hide_title_if_no_cats' => true,
     219        ) );
     220
     221        $this->assertNotContains( '<li class="categories">Categories', $found );
     222    }
     223
     224    /**
     225     * @ticket 33460
     226     */
     227    public function test_hide_title_if_no_cats_should_be_respected_for_empty_lists_when_false() {
     228        $found = wp_list_categories( array(
     229            'echo' => false,
     230            'hide_title_if_no_cats' => false,
     231        ) );
     232
     233        $this->assertContains( '<li class="categories">Categories', $found );
     234    }
     235
     236    /**
     237     * @ticket 33460
     238     */
     239    public function test_hide_title_if_no_cats_should_be_ignored_when_category_list_is_not_empty() {
     240        $cat = $this->factory->category->create();
     241
     242        $found = wp_list_categories( array(
     243            'echo' => false,
     244            'hide_empty' => false,
     245            'hide_title_if_no_cats' => true,
     246        ) );
     247
     248        $this->assertContains( '<li class="categories">Categories', $found );
     249    }
    200250}
Note: See TracChangeset for help on using the changeset viewer.