Metabox Data not saving

时间:2014-02-11 作者:dannyw24

保存页面后,我的Metabox数据不会显示,我在getextra\\u mbe\\u date和getextra\\u mbe\\u time上都尝试了vardump(),返回null

我的代码是

<?php
/*
Plugin Name: Events CPT
Plugin URI: ****
Description: This Plugins creates an events custom post type with a date and time metabox included.
Version: 0.01
Author: ***
Author URI: ****
License: GPLv2
*/
?>
<?php
function getextra_events_custom_post_type_create() {
  $labels = array(
    \'name\'               => \'Events\',
    \'singular_name\'      => \'Event\',
    \'add_new\'            => \'Add New Event\',
    \'add_new_item\'       => \'Add New Event\',
    \'edit_item\'          => \'Edit Event\',
    \'new_item\'           => \'New Event\',
    \'all_items\'          => \'All Events\',
    \'view_item\'          => \'View Event\',
    \'search_items\'       => \'Search Events\',
    \'not_found\'          => \'No Events found\',
    \'not_found_in_trash\' => \'No Eventss found in Trash\',
    \'parent_item_colon\'  => \'\',
    \'menu_name\'          => \'Events\'
  );

  $args = array(
    \'labels\'             => $labels,
    \'public\'             => true,
    \'publicly_queryable\' => true,
    \'show_ui\'            => true,
    \'show_in_menu\'       => true,
    \'query_var\'          => true,
    \'rewrite\'            => array( \'slug\' => \'events\' ),
    \'capability_type\'    => \'post\',
    \'has_archive\'        => true,
    \'hierarchical\'       => false,
    \'menu_position\'      => null,
    \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
  );
  register_post_type( \'Events\', $args );
}
add_action( \'init\', \'getextra_events_custom_post_type_create\' );
?>
<?php
//creation of the metabox happens here
add_action( \'add_meta_boxes\', \'getextra_mbe_create\' );
function getextra_mbe_create() {
    add_meta_box( \'getextra-meta-dt\', \'Event Date and Time\', \'getextra_mbe_dt_callback\', \'Events\', \'normal\', \'high\');
}
//function to display the form fields
function getextra_mbe_dt_callback($post) {
    //get the metadata values if they exist
    $getextra_mbe_date = get_post_meta ($post->ID, \'_getextra_mbe_date\', true);
    $getextra_mbe_time = get_post_meta ($post->ID, \'_getextra_mbe_time\', true);
    echo \'Please fill out the event time and date.\';
?>
    <p>Date: <input type="text" name="getextra_mbe_date" value="<?php esc_attr( $getextra_mbe_date); ?>" /></p>
    <p>Time: <input type="text" name="getextra_mbe_time" value="<?php esc_attr( $getextra_mbe_time); ?>" /> </p> 
<?php } 
    //hook to save the meta box data
    add_action (\'save_post\', \'getextra_mbe_save_dt\');

function getextra_mbe_save_dt( $post_id ) {

    //verify the metadata is set
    if (isset( $_POST[\'getextra_mbe_date\'])) {
        //save the metadata
        update_post_meta ($post_id, \'_getextra_mbe_date\', strip_tags($_POST[\'getextra_mbe_date\']));
        update_post_meta( $post_id, \'_getextra_mbe_time\', strip_tags($_POST[\'getextra_mbe_time\']));
    }

}
 ?>

1 个回复
最合适的回答,由SO网友:nawrockp 整理而成

我尝试了你的代码,它很好地保存了自定义字段。问题是esc\\u attr返回一个字符串,但不回显它。

请尝试以下操作:

<p>Date: <input type="text" name="getextra_mbe_date" value="<?php echo esc_attr( $getextra_mbe_date); ?>" /></p>
<p>Time: <input type="text" name="getextra_mbe_time" value="<?php echo esc_attr( $getextra_mbe_time); ?>" /> </p> 

结束

相关推荐

为什么要为index.php编写标记?

据我所知,索引。根据模板层次结构,当更具体的模板不可用时,将使用php(必需的模板)。我的问题是,如果我创建了主题使用的所有特定模板(例如home.php、single.php、page.php、search.php、archive.php、404.php等),那个么为什么还要在索引中写任何标记呢。php?我还是把它留白好吗?是否有理由填写索引。php?