如何添加"所有类别"分层wp_dropdown_categories()

时间:2017-01-29 02:13:26

标签: php wordpress

我教过如何在日语stackoverflow上对wp_dropdown_categories()进行分层。

class My_Walker_CategoryDropdown extends Walker_CategoryDropdown {
    public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        $pad = str_repeat( '-', $depth );

        /** This filter is documented in wp-includes/category-template.php */
        $cat_name = apply_filters( 'list_cats', $category->name, $category );

        if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
            $value_field = $args['value_field'];
        } else {
            $value_field = 'term_id';
        }

        $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";

        // Type-juggling causes false matches, so we force everything to a string.
        if ( (string) $category->{$value_field} === (string) $args['selected'] ) {
            $output .= ' selected="selected"';
        }
        $output .= '>';
        $output .= $pad . $cat_name;
        if ( $args['show_count'] ) {
            $output .= '&nbsp;&nbsp;(' . number_format_i18n( $category->count ) . ')';
        }
        $output .= "</option>\n";
    }

}

wp_dropdown_categories( [
    'depth'        => 3,
    'hierarchical' => true,
    'walker'       => new My_Walker_CategoryDropdown(),
] );

有了这个,我无法选择&#34;所有类别&#34;。我只能搜索特定的类别。 如何添加&#34;所有类别&#34;分层wp_dropdown_categories()

1 个答案:

答案 0 :(得分:0)

您可以将Runnable添加到'show_option_all' => __('All Categories', 'text-domain'),

wp_dropdown_categories