我最近一直在开发一个涉及自定义帖子类型和分类的插件。我现在正在努力的是让自定义帖子字段显示在“所有预订”中。屏幕。我可以让列显示,但不能在我的生活中获取列中的字段以填充数据库中的数据。
我的自定义邮政编码如下:
function reservations_init() {
$labels = array(
'name' => _x( 'Reservations', 'post type general name' ),
'singular_name' => _x( 'Reservation', 'post type singular name' ),
'add_new' => _x( 'Add New', 'reservation' ),
'add_new_item' => __( 'Add New Reservation' ),
'edit_item' => __( 'Edit Reservation' ),
'new_item' => __( 'New Reservation' ),
'all_items' => __( 'Reservations' ),
'view_item' => __( 'View Reservation' ),
'search_items' => __( 'Search Reservations' ),
'not_found' => __( 'No reservations found' ),
'not_found_in_trash' => __( 'No reservations found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Reservations'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our reservations',
'public' => true,
'menu_position' => 5,
'supports' => array( 'none' ),
'has_archive' => true,
'show_in_nav_menu' => true,
'show_in_menu' => 'edit.php?post_type=meals',
);
register_post_type( 'reservations', $args );
}
add_action( 'init', 'reservations_init' );
function restauranter_add_meta_box() {
$screens = array( 'reservations' );
foreach ( $screens as $screen ) {
add_meta_box(
'restauranter_firstname',
__( 'Reservation Details', 'restauranter' ),
'restauranter_reservation_details',
$screen,
'normal',
'high'
);
}
}
add_action( 'add_meta_boxes', 'restauranter_add_meta_box' );
function restauranter_reservation_details() {
global $post;
echo '<input type="hidden" name="reservationmeta_noncename" id="reservationmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$firstname = get_post_meta($post->ID, '_firstname', true);
$lastname = get_post_meta($post->ID, '_lastname', true);
$phone = get_post_meta($post->ID, '_phone', true);
$email = get_post_meta($post->ID, '_email', true);
$guests = get_post_meta($post->ID, '_guests', true);
$date = get_post_meta($post->ID, '_date', true);
$time = get_post_meta($post->ID, '_time', true);
echo '<p>First Name:</p>';
echo '<input type="text" name="_firstname" value="' . $firstname . '" class="widefat" />';
echo '<p>Last Name:</p>';
echo '<input type="text" name="_lastname" value="' . $lastname . '" class="widefat" />';
echo '<p>Phone Number:</p>';
echo '<input type="text" name="_phone" value="' . $phone . '" class="widefat" />';
echo '<p>Email:</p>';
echo '<input type="text" name="_email" value="' . $email . '" class="widefat" />';
echo '<p>Number of Guests:</p>';
echo '<input type="text" name="_guests" value="' . $guests . '" class="widefat" />';
echo '<p>Reservation Date:</p>';
echo '<input type="text" name="_date" value="' . $date . '" class="widefat date" />';
echo '<p>Reservation Time:</p>';
echo '<input type="text" name="_time" value="' . $time . '" class="widefat time" />';
echo '<input type="submit" name="_time" value="" class="widefat submit" />';
}
function restauranter_save_reservations_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['reservationmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
$reservations_meta['_firstname'] = $_POST['_firstname'];
$reservations_meta['_lastname'] = $_POST['_lastname'];
$reservations_meta['_phone'] = $_POST['_phone'];
$reservations_meta['_email'] = $_POST['_email'];
$reservations_meta['_guests'] = $_POST['_guests'];
$reservations_meta['_date'] = $_POST['_date'];
$reservations_meta['_time'] = $_POST['_time'];
foreach ($reservations_meta as $key => $value) {
if( $post->post_type == 'revision' ) return;
$value = implode(',', (array)$value);
if(get_post_meta($post->ID, $key, FALSE)) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key);
}
}
add_action('save_post', 'restauranter_save_reservations_meta', 1, 2); // save the custom fields
function add_new_reservations_columns($reservations_columns) {
$new_columns['cb'] = '<input type="checkbox" />';
$new_columns['_firstname'] = __('First Name', '_firstname');
return $new_columns;
}
add_filter('manage_edit-reservations_columns' , 'add_new_reservations_columns');
我已经尝试了谷歌搜索,我已经尝试了大量的代码示例,我已经尝试过用自己的代码玩,但仍然没有快乐。
答案 0 :(得分:1)
您还需要一个功能才能将数据添加到这些列中....
function custom_edit-reservations_columns( $column, $post_id ) {
switch ( $column ) {
case "_firstname":
$firstname = get_post_meta($post_id, '_firstname', true);
echo $firstname;
break;
}
}
add_action( "manage_posts_custom_column", "custom_edit-reservations_columns", 10, 2 );
希望有所帮助