我有一个针对某些自定义帖子类型的自定义元框,并且元框中的其中一个字段就像这里的下拉菜单! 这是我的代码
add_action('add_meta_boxes','mind_scholar_meta_box');
function mind_scholar_meta_box()
{
add_meta_box('ms_meta_box','Info Meta Box','mind_scholar_meta_fields','team','normal','high');
}
function mind_scholar_meta_fields()
{
global $post;
$field=get_post_meta($post->ID,'mind_scholar_key',true);
$select=get_post_meta($post->ID,'mind_scholar_select_key',true);
?>
<html>
<body>
<table>
<tr>
<td weidth="50%">
School Name:
</td>
<td weidth="50%">
<input type="text" name="first" value="<?php echo $field ?>">
</td>
</tr>
<tr>
<td width="50%">
Stars Rating:
</td>
<td>
<select>
<option value="star_1" <?php selected( $select, 'star_1' ); ?>>Star 1</option>
<option value="star_2" <?php selected( $select, 'star_2' ); ?>>Star 2</option>
<option value="star_3" <?php selected( $select, 'star_3' ); ?>>Star 3</option>
<option value="star_4" <?php selected( $select, 'star_4' ); ?>>Star 4</option>
<option value="star_5" <?php selected( $select, 'star_5' ); ?>>Star 5</option>
</select>
</td>
</tr>
</table>
</body>
</html>
<?php
}
add_action('save_post','save_posts');
function save_posts()
{
global $post;
$ms_field=$_POST['first'];
$ms_field_select=$_POST[''];
update_post_meta($post->ID,'mind_scholar_key',$ms_field);
update_post_meta($post->ID,'mind_scholar_select_key',$ms_field_select);
}
不幸的是,我的下拉菜单未保存在我的CPT中!有人可以帮忙吗? :(
答案 0 :(得分:2)
复制此代码并使用
add_action('add_meta_boxes','mind_scholar_meta_box'); function mind_scholar_meta_box() { add_meta_box('ms_meta_box','Info Meta Box','mind_scholar_meta_fields','team','normal','high'); } function mind_scholar_meta_fields() {
global $post; $field=get_post_meta($post->ID,'mind_scholar_key',true); $select=get_post_meta($post->ID,'mind_scholar_select_key',true); ?> <html> <body> <table>
<tr>
<td weidth="50%">
School Name:
</td>
<td weidth="50%">
<input type="text" name="first" value="<?php echo $field ?>">
</td>
</tr>
<tr>
<td width="50%">
Stars Rating:
</td>
<td>
<select name='second_select'>
<option value="star_1" <?php selected( $select, 'star_1' ); ?>>Star 1</option>
<option value="star_2" <?php selected( $select, 'star_2' ); ?>>Star 2</option>
<option value="star_3" <?php selected( $select, 'star_3' ); ?>>Star 3</option>
<option value="star_4" <?php selected( $select, 'star_4' ); ?>>Star 4</option>
<option value="star_5" <?php selected( $select, 'star_5' ); ?>>Star 5</option>
</select>
</td>
</tr> </table> </body> </html> <?php } add_action('save_post','save_posts'); function save_posts() { global $post; $ms_field=$_POST['first']; $ms_field_select=$_POST['second_select'];
update_post_meta($post->ID,'mind_scholar_key',$ms_field); update_post_meta($post->ID,'mind_scholar_select_key',$ms_field_select);
}
答案 1 :(得分:1)
您完全忘记将name
值添加到选择内容和$post
在元数据框渲染中:
<select name="ratings">
在save_posts()
函数
$ms_field_select = $_POST['ratings'];