簡體   English   中英

如何將“所有類別”添加到分層的wp_dropdown_categories()

[英]How can I add “All Category” to hierarchized wp_dropdown_categories()

我教過如何在日語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(),
] );

這樣,我不能選擇“所有類別”。 而且我只能搜索特定類別。 如何將“所有類別”添加到分層的wp_dropdown_categories()

您可以將'show_option_all' => __('All Categories', 'text-domain'),wp_dropdown_categories

wp_dropdown_categories( array(
    'show_option_all' => __('All Categories', 'text-domain'),
    'depth'        => 3,
    'hierarchical' => true,
    'walker'       => new My_Walker_CategoryDropdown(),
));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM