SETUP
我写了一段代码,在其中我创建了一个自定义帖子类型和一个带有aditional信息的元框,我的自定义帖子类型称为组合。此元框包含3个字段,一个链接字段,日期字段和专业知识字段,这些字段使用该自定义帖子类型进行存储和保存。
问题
当我更新我的投资组合项目(没有错误)时,这整个设置完全正常,但是一旦我开始在其他帖子类型中添加内容,例如我定义的页面,消息和其他帖子类型。我有错误和通知随处可见,因为它也试图将元框分配给这些帖子项目。虽然我认为我特意将Meta Box分配给名为投资组合的帖子类型,但我担心我在某处犯了错误。见代码。
因此,当我加载不同的posttype时,我会注意到它有一个未定义的索引
Notice: Undefined index: dbt_website-link in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
但是当我保存它时,我一次会遇到很多错误。除了通知,我还得到信息已发送到标题错误。
Notice: Undefined index: dbt_website-link in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
Notice: Undefined index: dbt_opleverdatum in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
Notice: Undefined index: dbt_text in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 161
Warning: Cannot modify header information - headers already sent by (output started at /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php:161) in /home/jellyf1q/public_html/test/webfanatics/wp/wp-admin/post.php on line 197
Warning: Cannot modify header information - headers already sent by (output started at /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php:161) in /home/jellyf1q/public_html/test/webfanatics/wp/wp-includes/pluggable.php on line 1228
ASSUMPTION
通过一些演绎,我认为这与尝试应用于所有帖子类型的元框有关。我还不知道如何解决这个问题,因为我已经在代码中告诉它只使用帖子类型组合,因为我按照this tutorial上的说明操作。它实际上遵循该指令,因为自定义元框仅应用于投资组合CPT,但代码中发生了什么使得它寻找元数据字段的post变量的索引。
CODE
我在这里为metabox提供了代码。正如您在第5行的元数据数组中看到的那样,我告诉它分配给帖子类型组合
$prefix = 'dbt_';
$meta_box = array(
'id' => 'meta-portfolio',
'title' => 'Overige informatie',
'page' => 'portfolio',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Website link',
'desc' => 'De link naar het portfolio item bij onze klant',
'id' => $prefix . 'website-link',
'type' => 'text',
'std' => 'http://'
),
array(
'name' => 'Opleverdatum',
'desc' => 'De opleverdatum van het project',
'id' => $prefix . 'opleverdatum',
'type' => 'text',
'std' => 'dd/mm/yyyy'
),
array(
'name' => 'Expertise',
'desc' => 'De Expertise die is gebruikt binnen het project',
'id' => $prefix . 'text',
'type' => 'text',
'std' => 'Expertise'
)
)
);
add_action('admin_menu', 'portfolio_add_box');
// Add meta box
function portfolio_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'portfolio_show_box', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}
// Callback function to show fields in meta box
function portfolio_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="portfolio_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '</td><td>',
'</td></tr>';
}
echo '</table>';
}
add_action('save_post', 'portfolio_save_data');
// Save data from meta box
function portfolio_save_data($post_id) {
global $meta_box;
// verify nonce
if (isset($_POST['portfolio_meta_box_nonce']) && !wp_verify_nonce($_POST['portfolio_meta_box_nonce'], basename(__FILE__))) {
return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
?>
自定义帖子类型的代码如下。
add_action('init', 'portfolio_register');
function portfolio_register() {
$labels = array(
'name' => _x('portfolio', 'post type general name'),
'singular_name' => _x('portfolio', 'post type singular name'),
'add_new' => _x('Nieuw', 'portfolio'),
'add_new_item' => __('Nieuw portfolio'),
'edit_item' => __('Edit portfolio'),
'new_item' => __('Nieuw portfolio'),
'view_item' => __('View portfolio'),
'search_items' => __('Search portfolio'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => null,
'rewrite' => array(
'slug' => 'portfolio',
'with_front' => false,
'hierarchical' => true),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail'),
'menu_icon' => 'dashicons-format-image',
'taxonomies' => array('post_tag')
);
register_post_type( 'portfolio' , $args );
}
第161行是指元框中的这段代码。
foreach ($meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
答案 0 :(得分:3)
此挂钩add_action('save_post', 'portfolio_save_data');
用于保存所有帖子,页面和custom_post_types,不仅适用于CPT。
因此,当你添加新的常规帖子时,这个函数也会运行,但是没有元数据和类似的东西,有些甚至没有$_POST
。
在检查权限之前的函数portfolio_save_data
中,您可以执行以下检查:
if ( 'portfolio' !== get_post_type() ) {
return $post_id;
}
所以你的保存仅在你的CPT中运行,否则让WP做它的事情。
就像我上面提到的,有时有时在$ _POST中没有这样的键'post_type'
,所以你可以改变这一行来检查权限:
if ( isset($_POST['post_type']) && 'page' == $_POST['post_type'])
最后,根据Codex,当您使用add_meta_boxes
注册元数据时,最好挂钩portfolio_add_box
。