在下拉列表中显示自定义POST类型,选项为“All”

时间:2021-03-30 作者:Juraj

wp_dropdown_categories() 函数有一个参数show_option_all 如果选中,则在所有术语中搜索。我的自定义帖子类型选择中需要类似的内容:

<form method="GET" action="<?php echo esc_url( home_url() ); ?>">   
    <div class="select-wrap">
        <label>Select location</label>

        <?php 
        $args = array(
            \'show_option_all\' => \'All locations\',
            \'hierarchical\' => 1,
            \'hide_empty\' => 0,
            \'orderby\' => \'name\',
            \'echo\' => 1,
            \'value_field\' => \'slug\',
        );

        $args[\'taxonomy\'] = \'location\';
        $args[\'class\'] = \'select--field\';

        wp_dropdown_categories( $args ); 
        ?>

    </div>

    <div class="select-wrap">
        <label>Select service</label>

        <select class="select--field">

            <?php
            $args = array(
                    \'post_type\' => \'service\',
                    \'posts_per_page\' => -1
            );

            $query = new WP_Query( $args );

            while ( $query->have_posts() ) : $query->the_post();
                    echo \'<option value="\' . get_the_ID() . \'">\' . get_the_title() . \'</option>\';
            endwhile;
            wp_reset_postdata();
            ?>

        </select> 
    </div>

    <input type="hidden" name="taxonomy" value="location">
    <input type="hidden" name="post_type" value="service">

    <button type="submit" class="search-btn btn--lg">Submit</button>
</form> 

1 个回复
SO网友:Nikolay

在第一个位置添加空选项:

...
echo \'<option value="">All locations</option>\';
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
    echo \'<option value="\' . get_the_ID() . \'">\' . get_the_title() . \'</option>\';
endwhile;
...

相关推荐