我想更新我的选择选项(下拉列表),但我不知道哪里出错了。这是我的密码。
<?php
function display_post_options(){
wp_nonce_field( basename( __FILE__ ), \'post_options_nonce\' );
?>
<form action=\'\' id=\'post-options-form\' method=\'post\'>
Choose Your Post Layout:
<select name=\'post_options_select\'>
<option id=\'right-sidebar\' value=\'right\' name=\'right_sidebar\' <?php selected(\'post_options_select\', \'right\'); ?> >Right sidebar</option>
<option id=\'left-sidebar\' value="left" name=\'left_sidebar\' <?php selected(\'post_options_select\', \'left\'); ?>>Left Sidebar</option>
<option id=\'no-sidebar\' value="no" name=\'no_sidebar\' <?php selected(\'post_options_select\', \'no\'); ?>>No Sidebar</option>
</select>
</form>
<?php
}
function post_options(){
add_meta_box(
\'post-options\',
\'Post Options\',
\'display_post_options\',
\'post\',
\'advanced\',
\'high\',
$callback_args
);
}
function save_post_options($post_id){
if (if_user_can_save($post_id, \'post_options_nonce\')) {
update_post_meta($post_id, \'post_options_select\', $_POST[\'post_options_select\']);
}
}
function if_user_can_save($post_id, $nonce){
//Check if post is autosave
$is_autosave = wp_is_post_autosave($post_id);
//Check if it is a Revision
$is_revision = wp_is_post_revision($post_id);
//Is the nonce valid
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], basename( __FILE__ ) ) );
return !($is_autosave || $is_revision) && $is_valid_nonce;
}
add_action(\'add_meta_boxes\', \'post_options\');
add_action( \'save_post\', \'save_post_options\', 10, 2 );
?>
更新:(请参阅注释3)这是我将html分配给变量的地方。但它会输出(selected=\'selected\'选择您的帖子布局:[选择列表])
function display_post_options(){
global $post;
wp_nonce_field( basename( __FILE__ ), \'post_options_nonce\' );
$post_options_select_value = get_post_meta($post->ID,\'post_options_select\',true);
$html = \'Choose Your Post Layout: \';
$html .= \'<select name="post_options_select">\';
$html .= \'<option id="right-sidebar" value="right" name="right_sidebar"\' . selected($post_options_select_value, \'right\') . \'">Right sidebar</option>\';
$html .= \'<option id="left-sidebar" value="left" name="left_sidebar"\' . selected($post_options_select_value, \'left\') . \'" >Left Sidebar</option>\';
$html .= \'<option id="no-sidebar" value="no" name="no_sidebar"\' . selected($post_options_select_value, \'no\') . \'">No Sidebar</option>\';
$html .= \'</select>\';
echo $html;
}
最合适的回答,由SO网友:Waseem Abu Senjer 整理而成
首先,您不必在metabox中添加“form”html标记。
您没有将“$post”参数传递给函数“display\\u post\\u options”,并且您也错误地使用了selected()函数。这是我更新后的代码。
function display_post_options( $post){
wp_nonce_field( basename( __FILE__ ), \'post_options_nonce\' );
$post_options_select_value = get_post_meta($post->ID,\'post_options_select\',true);
?>
Choose Your Post Layout:
<select name=\'post_options_select\'>
<option id=\'right-sidebar\' value=\'right\' name=\'right_sidebar\' <?php selected($post_options_select_value, \'right\'); ?> >Right sidebar</option>
<option id=\'left-sidebar\' value="left" name=\'left_sidebar\' <?php selected($post_options_select_value, \'left\'); ?>>Left Sidebar</option>
<option id=\'no-sidebar\' value="no" name=\'no_sidebar\' <?php selected($post_options_select_value, \'no\'); ?>>No Sidebar</option>
</select>
实际上,您需要传递meta的值而不是键,以便“selected()”函数正常工作。
您还需要更改此行:
add_action( \'save_post\', \'save_post_options\', 10, 2 );
至
add_action( \'save_post\', \'save_post_options\', 10 );
在您的情况下,只需将一个参数传递给“save\\u post\\u options”函数。
我希望这会有帮助。