admin管理员组文章数量:1193564
My custom Meta Box has a drop down field, but how do I set the default Option Value to be blank? It is currently populating via the CPT's meta_value, but I'd like that to change.
Here's a screenshot of the issue:
I want the first option for the Second Author field to be blank/have no selection.
Code for Meta Box:
case 'select' :
$author_select_query = new WP_Query(array(
'post_type' => 'people',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'role',
'field' => 'slug',
'terms' => array('adjunct-faculty'),
),
array(
'taxonomy' => 'role',
'field' => 'slug',
'terms' => array('faculty'),
)
),
'meta_key' => 'ecpt_people_alpha',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => '-1'));
$authors = $author_select_query ->get_posts();
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach($authors as $author) {
echo '<option value="' . $author->ID . '"', $meta == $author->ID ? ' selected="selected"' : '', '>', $author->post_title, '</option>';
}
echo '</select>';
My custom Meta Box has a drop down field, but how do I set the default Option Value to be blank? It is currently populating via the CPT's meta_value, but I'd like that to change.
Here's a screenshot of the issue:
I want the first option for the Second Author field to be blank/have no selection.
Code for Meta Box:
case 'select' :
$author_select_query = new WP_Query(array(
'post_type' => 'people',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'role',
'field' => 'slug',
'terms' => array('adjunct-faculty'),
),
array(
'taxonomy' => 'role',
'field' => 'slug',
'terms' => array('faculty'),
)
),
'meta_key' => 'ecpt_people_alpha',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => '-1'));
$authors = $author_select_query ->get_posts();
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach($authors as $author) {
echo '<option value="' . $author->ID . '"', $meta == $author->ID ? ' selected="selected"' : '', '>', $author->post_title, '</option>';
}
echo '</select>';
Share
Improve this question
asked Nov 3, 2014 at 17:44
timmygtimmyg
1251 gold badge2 silver badges16 bronze badges
2 Answers
Reset to default 2Before the foreach loop:
foreach($authors as $author) {
Add this empty option element:
echo '<option value="0"></option>';
Hope it works! :)
Building on the accepted answer, adding this worked for me:
<option value="" selected="selected"></option>
The empty value worked for me, and I was able to identify when there was nothing selected and allowed the users to select nothing again in case they made a wrong selection.
本文标签: custom post typesSet Default Option Value as Blank for Meta Box
版权声明:本文标题:custom post types - Set Default Option Value as Blank for Meta Box 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738455050a2087706.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论