2013-04-13 34 views
0

我最近決定嘗試開發一個wordpress網站,用於存儲「摩托車」的自定義帖子類型。 而不是使用自定義字段,我寧願使用自定義元框對任何人 商店摩托車信息,比如在網站前端要顯示的速度。我的WordPress的自定義帖子和元框不保存或顯示元框值?

下面是代碼,通知我的,因爲我沒有使用一個隨機數和元數據保存功能,所以,這是因爲我一直在試圖把它簡單地保存元框輸入。

這裏是我的代碼:

function kc_mgb_initate(){ 
#this function is the first function that will be called to initiate our plugiin 
#this will be incharge of creating the Menu and Submenu's, as well as initate the custom post's, meta box's and taxonomies 
#Hang on, this plugin does not need options so no need for menus, skepping to creating custom posts 

//add_menu_page('Motorbike_Garage', 'Motor Bike Garage'); #the function that registers the menu pages 

#first create the labels for our custom post 

$customlabels = array(
       'name' => 'Motorbike Garage', 
       'singular_name' => 'Motorbike Garage', 
       'add_new' => 'Add New Motorbike', 
       'add_new_item' => 'Add New Motorbike', 
       'edit' => 'Edit', 
       'edit_item' => 'Edit Motorbikes', 
       'new_item' => 'New Motorbikes', 
       'view' => 'View', 
       'view_item' => 'View Motorbikes', 
       'search_items' => 'Search Motorbikes', 
       'not_found' => 'No Motorbikes found', 
       'not_found_in_trash' => 'No Motorbikes found in Trash', 
       'parent' => 'Parent Motorbike Review' 
      ); 

#array for our custom supports 
$customsupports = array('title', 'editor', 'comments', 'thumbnail', 'custom-fields'); 

#now a new array for our custom post args 

$customargs = array('labels' => $customlabels, 'public' => true, 'menu_position' => 5, 'supports' => $customsupports, 'taxonomies' => array('motorbike_category'), 'has_archive' => true, 'register_meta_box_cb' => 'add_motorbikes_metaboxes' 
); #this function registers the new custom post's 
#the 'register_meta_box_cb' => 'add_motorbikes_metaboxes' adds the meta box information needed for this post by calling the add_motorbikes_metaboxes function 


#now for the custom taxonomy we shall call 'motorbike_categories' 

#the variables 

$taxonomylabels = array('name' => _x('motorbikes', 'taxonomy general name'), 
      'singular_name' => _x('motorbike', 'taxonomy singular name'), 
      'search_items' => __('Search Motorbikes'), 
      'all_items' => __('All Motorbikes'), 
      'parent_item' => __('Parent Motorbike'), 
      'parent_item_colon' => __('Parent Motorbike:'), 
      'edit_item' => __('Edit Motorbike'), 
      'update_item' => __('Update Motorbike'), 
      'add_new_item' => __('Add New Motorbike'), 
      'new_item_name' => __('New Motorbike Name'), 
      'menu_name' => __('Motorbike Categories'), 
     ); 

$taxonomyslugs = array('slug' => 'motorbikes', 'hierarchical' => true, 'with_front' => false); 

#our function to add the taxonomy 

register_taxonomy('motorbike_category', 'post', array('hierarchical' => true, 'labels' => $taxonomylabels, 'rewrite' => $taxonomyslugs)); 
register_post_type('motorbikes', $customargs); 
} 

function add_motorbikes_metaboxes(){ 
#this adds the metabox information for our meta data 
add_meta_box('motorbikegaragedata', 'Motorbike Garage Information', 'create_motorbike_meta_box', 'motorbikes', 'side', 'default'); 
} 

function create_motorbike_meta_box($post, $box){ 
#this function will create the form for us 

#we need year, top mph, weight, seat height, engine in cc, power and 1/4 mile 
$mbg_top_mph = get_post_meta($post->ID, 'mbg_top_mph', true); 
$mbg_year = get_post_meta($post->ID, 'mbg_year', true); 
$mbg_weight = get_post_meta($post->ID, 'mbg_weight', true); 
$mbg_seat_height = get_post_meta($post->ID, 'mbg_seat_height', true); 
$mbg_engine = get_post_meta($post->ID, 'mbg_engine', true); 
$mbg_power = get_post_meta($post->ID, 'mbg_power', true); 
$mbg_sec = get_post_meta($post->ID, 'mbg_sec', true); 
?> 
<p> Motorbike MPH </p> 
<input type="text" name="mbg_top_mph" value="<?php echo $mbg_top_mph; ?>" /> <br /> 
<p> Motorbike Year </p> 
<input type="text" name="mbg_year" value="<?php echo $mbg_year; ?>" /> <br /> 
<p> Motorbike Weight(KG) </p> 
<input type="text" name="mbg_weight" value="<?php echo $mbg_weight; ?>" /> <br /> 
<p> Motorbike Seat Height(CM) </p> 
<input type="text" name="mbg_seat_height" value="<?php echo $mbg_seat_height; ?>" /> <br /> 
<p> Motorbike Engine(cc)</p> 
<input type="text" name="mbg_engine" value="<?php echo $mbg_engine; ?>" /><br /> 
<p> Motorbike Power(BHP) </p> 
<input type="text" name="mbg_power" value="<?php echo $mbg_power; ?>" /><br /> 
<p> Motorbike 1/4 Mile(SECs) </p> 
<input type="text" name="mbg_sec" value="<?php echo $mbg_sec; ?>" /> 
<?php 
} 

function save_motorbikes_metaboxes($post_id, $post){ 
#this saves the motorbike meta data 
update_post_meta($post->ID, 'mbg_top_mph', $_POST['mbg_top_mph']); 
update_post_meta($post->ID, 'mbg_year', $_POST['mbg_year']); 
update_post_meta($post->ID, 'mbg_weight', $_POST['mbg_weight']); 
update_post_meta($post->ID, 'mbg_seat_height', $_POST['mbg_seat_height']); 
update_post_meta($post->ID, 'mbg_engine', $_POST['mbg_engine']); 
update_post_meta($post->ID, 'mbg_power', $_POST['mbg_power']); 
update_post_meta($post->ID, 'mbg_sec', $_POST['mbg_sec']); 
} 

#this is the action that will now call the function to add our custom posts 
add_action('init', 'kc_mgb_initate'); # 'init' is when wordpresss has initiated properly 

add_action('save_post', 'save_motorbikes_metaboxes'); #this is too call the save function 

我檢查我的變量名等等,我只是堅持工作了爲什麼它是 不是救了我的meta框的信息。

任何提示,或者你有信息將非常感激。謝謝。

+0

爲了您自己的利益,請正確格式化並縮進代碼。像這樣讀取是一種痛苦,並且使調試更加困難。 [比較差異](http://wordpress.stackexchange.com/a/94716/12615)。 – brasofilo

回答

1

您從不將add_motorbikes_metaboxessave_motorbikes_metaboxes連接到任何鉤子(應分別爲add_meta_boxessave_post),以免它們被調用。

+0

有一個'register_meta_box_cb'說法(我不知道)註冊'add_motorbikes_metaboxes'。但是,是的,'save_post'缺失並且是代碼中的錯誤。 – brasofilo

+0

您好,感謝您的反饋,但是在我的實際代碼中,保存後添加操作就在那裏。我已經更新了我的代碼,所以你可以看到但它仍然沒有工作。 – afrokid