Я создал мета-ящик. Код:Как сохранить значение meta box?
// Create your custom meta box
add_action('add_meta_boxes', 'hotel_amenities');
// Add a custom meta box to a post
function hotel_amenities($post) {
add_meta_box(
'Meta Box Amenities', // ID, should be a string
'Amenities', // Meta Box Title
'amenities_content', // Your call back function, this is where your form field will go
'post', // The post type you want this to show up on, can be post, page, or custom post type
'normal', // The placement of your meta box, can be normal or side
'high' // The priority in which this will be displayed
);
}
// Content for the custom meta box
function amenities_content($post) {
echo '<label>Bed room</label>';
echo '<input type="text" name="amenity_bed_room" value="" />';
}
// Save your meta box content
add_action('save_post', 'save_amenities');
// save newsletter content
function save_amenities(){
global $post;
// Get our form field
if($_POST) :
$amenities_meta = esc_attr($_POST['amenity_bed_room']);
// Update post meta
update_post_meta($post->ID, '_amenities_custom_meta', $amenities_meta);
endif;
}
Он показывает мета-окно на странице сообщения администратора с текстовым полем. но он становится пустым, если я сохраняю или обновляю сообщение после того, как поместил что-нибудь в текстовое поле.
Кажется function save_amenities()
не работает. Что я делаю неправильно в этом коде?
Также для получения этого значения я использую функцию ниже. Это верно?
//get amenities meta box values
function get_amenities_meta_box() {
global $post;
$meta_values = get_post_meta($post->ID, '_amenities_custom_meta', true);
}
[примеры] (http://wordpress.stackexchange.com/search?tab=votes&pagesize=50&q=%2bsave_post%20%2badd_meta_boxes%20is% 3aanswer) – brasofilo
Вам не хватает 'if (определено ('DOING_AUTOSAVE') && DOING_AUTOS AVE) {return;} ', здесь есть пример: http://codex.wordpress.org/Function_Reference/add_meta_box –
Hah. Слово к «мясной коробке». – sbeliv01