0

我最近决定尝试开发一个带有自定义帖子类型的 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

我已经检查了我的变量名,所以,我只是想弄清楚为什么它没有保存我的元框信息。

您拥有的任何提示或信息将不胜感激。谢谢你。

4

1 回答 1

1

您永远不会连接add_motorbikes_metaboxes或连接save_motorbikes_metaboxes到任何钩子(应该分别是add_meta_boxessave_post),因此它们永远不会被调用。

于 2013-04-14T00:26:31.967 回答