我已经创建了一个元框,它有一个选择下拉列表,我想在用户从元框中选择特定选项时在帖子中显示它。我想不出是怎么回事。这是我的元框代码。
<?php
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);
?>
<label for="options-select">Choose Your Post Layout:</label>
<select name=\'post_options_select\' id="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>
<?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;
}
function display_content( $content ){
$test = get_post_meta(get_the_ID(), \'options-select\', true);
$content .= $test;
print_r($test);
return $content;
}
add_action(\'add_meta_boxes\', \'post_options\');
add_action( \'save_post\', \'save_post_options\', 10, 2 );
add_action(\'the_content\', \'display_content\');
?>