У меня есть приведенный ниже код для добавления двух отдельных мета-полей к моему пользовательскому типу сообщений. Изначально у меня было только мета-окно «отзывы», которое работало нормально. Теперь я добавил «кредиты», которые нормально отображаются в администраторе, но, похоже, не сохраняются в базе данных?
Может кто-нибудь помочь, надеюсь, в этом нет ничего серьезного .
// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_project_metaboxes' );
// Register the Project Meta Boxes
function add_project_metaboxes() {
add_meta_box('oak_testimonial', 'Testimonial', 'oak_testimonial', 'project', 'normal', 'default');
add_meta_box('oak_credits', 'Credits', 'oak_credits', 'project', 'side', 'default');
}
// Add Testimonial Metabox
function oak_testimonial() {
global $post;
// Noncename needed to verify where the data originated
echo '';
// Get the testimonial data if its already been entered
$quote = get_post_meta($post->ID, '_quote', true);
$name = get_post_meta($post->ID, '_name', true);
$title = get_post_meta($post->ID, '_title', true);
$company = get_post_meta($post->ID, '_company', true);
// Echo out the field
echo '
Name:
';
echo '';
echo '
Job Title:
';
echo '';
echo '
Company:
';
echo '';
echo '
Quote:
';
echo '';
}
// Add Credits Metabox
function oak_credits() {
global $post;
// Noncename needed to verify where the data originated
echo '';
// Get the credits data if its already been entered
$role1 = get_post_meta($post->ID, '_role1', true);
$name1 = get_post_meta($post->ID, '_name1', true);
$role2 = get_post_meta($post->ID, '_role2', true);
$name2 = get_post_meta($post->ID, '_name2', true);
$role3 = get_post_meta($post->ID, '_role3', true);
$name3 = get_post_meta($post->ID, '_name3', true);
// Echo out the field
echo '
Role:
';
echo '';
echo '
Name:
';
echo '';
echo '
Role:
';
echo '';
echo '
Name:
';
echo '';
echo '
Role:
';
echo '';
echo '
Name:
';
echo '';
}
// Save the Metabox Data
function oak_save_project_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['testimonialmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !wp_verify_nonce( $_POST['creditsmeta_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.
$testimonial_meta['_quote'] = $_POST['_quote'];
$testimonial_meta['_name'] = $_POST['_name'];
$testimonial_meta['_title'] = $_POST['_title'];
$testimonial_meta['_company'] = $_POST['_company'];
$credit_meta['_role1'] = $_POST['_role1'];
$credit_meta['_name1'] = $_POST['_name1'];
$credit_meta['_role2'] = $_POST['_role2'];
$credit_meta['_name2'] = $_POST['_name2'];
$credit_meta['_role3'] = $_POST['_role3'];
$credit_meta['_name3'] = $_POST['_name3'];
// Add values of $testimonial_meta as custom fields
foreach ($testimonial_meta as $key => $value) { // Cycle through the $testimonial_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
}
// Add values of $credit_meta as custom fields
foreach ($credit_meta as $key => $value) { // Cycle through the $testimonial_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
}
}
add_action('save_post', 'oak_save_project_meta', 1, 2); // save the custom fields
Подробнее здесь: https://stackoverflow.com/questions/278 ... g-all-data
Пользовательский мета-блок Wordpress не сохраняет все данные ⇐ Php
Кемеровские программисты php общаются здесь
-
Anonymous
1728895930
Anonymous
У меня есть приведенный ниже код для добавления двух отдельных мета-полей к моему пользовательскому типу сообщений. Изначально у меня было только мета-окно «отзывы», которое работало нормально. Теперь я добавил «кредиты», которые нормально отображаются в администраторе, но, похоже, не сохраняются в базе данных?
Может кто-нибудь помочь, надеюсь, в этом нет ничего серьезного .
// Allow registering of custom meta boxes
add_action( 'add_meta_boxes', 'add_project_metaboxes' );
// Register the Project Meta Boxes
function add_project_metaboxes() {
add_meta_box('oak_testimonial', 'Testimonial', 'oak_testimonial', 'project', 'normal', 'default');
add_meta_box('oak_credits', 'Credits', 'oak_credits', 'project', 'side', 'default');
}
// Add Testimonial Metabox
function oak_testimonial() {
global $post;
// Noncename needed to verify where the data originated
echo '';
// Get the testimonial data if its already been entered
$quote = get_post_meta($post->ID, '_quote', true);
$name = get_post_meta($post->ID, '_name', true);
$title = get_post_meta($post->ID, '_title', true);
$company = get_post_meta($post->ID, '_company', true);
// Echo out the field
echo '
Name:
';
echo '';
echo '
Job Title:
';
echo '';
echo '
Company:
';
echo '';
echo '
Quote:
';
echo '';
}
// Add Credits Metabox
function oak_credits() {
global $post;
// Noncename needed to verify where the data originated
echo '';
// Get the credits data if its already been entered
$role1 = get_post_meta($post->ID, '_role1', true);
$name1 = get_post_meta($post->ID, '_name1', true);
$role2 = get_post_meta($post->ID, '_role2', true);
$name2 = get_post_meta($post->ID, '_name2', true);
$role3 = get_post_meta($post->ID, '_role3', true);
$name3 = get_post_meta($post->ID, '_name3', true);
// Echo out the field
echo '
Role:
';
echo '';
echo '
Name:
';
echo '';
echo '
Role:
';
echo '';
echo '
Name:
';
echo '';
echo '
Role:
';
echo '';
echo '
Name:
';
echo '';
}
// Save the Metabox Data
function oak_save_project_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['testimonialmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !wp_verify_nonce( $_POST['creditsmeta_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.
$testimonial_meta['_quote'] = $_POST['_quote'];
$testimonial_meta['_name'] = $_POST['_name'];
$testimonial_meta['_title'] = $_POST['_title'];
$testimonial_meta['_company'] = $_POST['_company'];
$credit_meta['_role1'] = $_POST['_role1'];
$credit_meta['_name1'] = $_POST['_name1'];
$credit_meta['_role2'] = $_POST['_role2'];
$credit_meta['_name2'] = $_POST['_name2'];
$credit_meta['_role3'] = $_POST['_role3'];
$credit_meta['_name3'] = $_POST['_name3'];
// Add values of $testimonial_meta as custom fields
foreach ($testimonial_meta as $key => $value) { // Cycle through the $testimonial_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
}
// Add values of $credit_meta as custom fields
foreach ($credit_meta as $key => $value) { // Cycle through the $testimonial_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
}
}
add_action('save_post', 'oak_save_project_meta', 1, 2); // save the custom fields
Подробнее здесь: [url]https://stackoverflow.com/questions/27839196/wordpress-custom-meta-box-not-saving-all-data[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия