Я создал пользовательский пост станд ewithin WordPress под названием «актеров»Wordpress PHP Как добавить и сохранить значение отборного выпадающего меню для пользовательского почтового типа
В этом таможенном посте типа, я добавил некоторые метабоксы, где admin может добавлять пользовательские данные обо всех актерах.
Мне нужно, чтобы один из этих обменов был выбранным входом, а не текстовым вводом, однако я не могу понять, как сохранить значение и показать его как выбранное.
Вот мой текущий код PHP:
// Add the Actors Meta Boxes
function add_actor_metaboxes() {
add_meta_box('actors_info', 'Actor Info', 'Nial_Actors::actors_info', 'actors', 'normal', 'default');
}
// The Actors Metabox
function actors_info() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="actorinfometa_noncename" id="actorinfometa_noncename" value="' .
wp_create_nonce(plugin_basename(__FILE__)) . '" />';
// Get the location data if its already been entered
$spotlight_url = get_post_meta($post->ID, '_spotlight_url', true);
$actor_gender = get_post_meta($post->ID, '_actor_gender', true);
$actor_age = get_post_meta($post->ID, '_actor_age', true);
$actor_height = get_post_meta($post->ID, '_actor_height', true);
$actor_weight = get_post_meta($post->ID, '_actor_weight', true);
// Echo out the field
echo '<p>Spotlight URL</p>';
echo '<input type="text" name="_spotlight_url" value="' . $spotlight_url . '" class="widefat" />';
echo '<p>Gender</p>';
echo '<select name="_actor_gender" id="actor_gender">';
echo '<option value="null" ' . selected($actor_gender, 'null') . '>--</option>';
echo '<option value="male" ' . selected($actor_gender, 'male') . '>Male</option>';
echo '<option value="female" ' . selected($actor_gender, 'female') . '>Female</option>';
echo '</select>';
echo '<p>Age</p>';
echo '<input type="text" name="_actor_age" value="' . $actor_age . '" class="widefat" />';
echo '<p>Height</p>';
echo '<input type="text" name="_actor_height" value="' . $actor_height . '" class="widefat" />';
echo '<p>Weight</p>';
echo '<input type="text" name="_actor_weight" value="' . $actor_weight . '" class="widefat" />';
}
// Save the Metabox Data
function actor_info_save($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (!isset($_POST['actorinfometa_noncename']) || !wp_verify_nonce($_POST['actorinfometa_noncename'], basename(__FILE__))) {
return $post->ID;
}
//if (!wp_verify_nonce($_POST['actorinfometa_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;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$actor_meta['_spotlight_url'] = $_POST['_spotlight_url'];
$actor_meta['_actor_gender'] = $_POST['_actor_gender'];
$actor_meta['_actor_age'] = $_POST['_actor_age'];
$actor_meta['_actor_height'] = $_POST['_actor_height'];
$actor_meta['_actor_weight'] = $_POST['_actor_weight'];
// Add values of $actor_meta as custom fields
foreach ($actor_meta as $key => $value) { // Cycle through the $actor_meta array!
if($post->post_type == 'revision') return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
Есть ли что-то очевидно, я здесь отсутствует - Я хочу, чтобы опция «Пол», чтобы быть выпадающий выбора входного сигнала
Что приходит через в данных $ _POST для _actor_gender? Было бы хорошо знать, является ли это проблемой получения или сохранения. –
Я думаю, что это может быть проблема с сохранением, как когда я повторяю $ _POST ['_ actor_gender']; in actors_info() Я получаю Undefined index: _actor_gender –
Другие значения save ok? Попробуйте сохранить сообщение meta с помощью AJAX, таким образом вы увидите детали переменной '$ _POST'. –