首页 文章

我的wordpress自定义帖子和元框不保存或显示元框值?

提问于
浏览
0

我最近决定尝试开发一个自定义帖子类型的wordpress网站,用于存储“摩托车” . 我宁愿使用自定义元框来存储摩托车信息,而不是使用自定义字段,例如在网站前端显示的速度 .

这是代码,我的保存函数的通知我没有使用nonce的元数据,所以,这是因为我一直试图让它只是保存元框输入 .

这是我的代码:

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; ?>" /> 
<p> Motorbike Year </p> <input type="text" name="mbg_year" value="<?php echo $mbg_year; ?>" />
<p> Motorbike Weight(KG) </p> <input type="text" name="mbg_weight" value="<?php echo $mbg_weight; ?>" />
<p> Motorbike Seat Height(CM) </p> <input type="text" name="mbg_seat_height" value="<?php echo $mbg_seat_height; ?>" />
<p> Motorbike Engine(cc)</p> <input type="text" name="mbg_engine" value="<?php echo $mbg_engine; ?>" />
<p> Motorbike Power(BHP) </p> <input type="text" name="mbg_power" value="<?php echo $mbg_power; ?>" />
<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

我检查了我的变量名称,所以,我只是坚持解决为什么它不保存我的元信息 .

任何提示或信息都会非常感激 . 谢谢 .

1 回答

  • 1

    你永远不会将 add_motorbikes_metaboxessave_motorbikes_metaboxes 连接到任何钩子(分别应该是 add_meta_boxessave_post ),因此它们永远不会被调用 .

相关问题