开发者

How can i add a new field inside wp_dropdown_categories in WordPress?

开发者 https://www.devze.com 2023-03-18 04:50 出处:网络
Here now create a WordPress category drop down menu, then this category menu display the default category list, but need a custom filed should be display in this menu list. Now i use this code for cat

Here now create a WordPress category drop down menu, then this category menu display the default category list, but need a custom filed should be display in this menu list. Now i use this code for category menu.

<?php 
    wp_dropdown_categories( array(
   'name' => $this->get_field_name( 'category' ),
   'selected' => $instance["category"],
   ) );
?>

Here i need add a custom menu option "all".

<select class="postform" id="widget-categoryposts[3][cat]" name="widget-categoryposts[3][cat]">
    <option selected="selected" value="1" class="level-0">Uncategorize开发者_运维知识库d</option>
    <option value="18" class="level-0">thumb</option>
    <option value="19" class="level-0">snake</option>
    <option value="all" class="level-0">all</option>
</select>


You can use it in this way:

<?php 
wp_dropdown_categories( array(
    'name' => $this->get_field_name( 'category' ),
    'selected' => $instance["category"],
    'show_option_all' => 'All'
) );
?>

Where the "all" option will appears with the text you specify in the 'show_option_all' element and with the value "0" in the Drop Down field.


Yep leticia is right. Here's my code for better control of output.

 <?php 
    $args = array(
                    'name'             => $this->get_field_name('category'),
        'show_option_none' => __( 'Select category' ),
        'show_count'       => 1,
        'orderby'          => 'name',
        'echo'             => 0,
                    'selected'         => $category,
                    'class'            => 'widefat'
    );
            echo wp_dropdown_categories($args);
        ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号