我目前正致力于创建元框。我使用了以下教程和一些自适应的。教程链接:http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/
现在我收到以下错误消息:
注意:未定义索引:dsmeta_image in /customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php 第181行注意:未定义的索引:dsmeta_image_caption in /customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php 在第181行
似乎该变量不存在,我正在使用Metabox的数组字段并创建一个foreach循环,如果我理解正确的话,请引导您完成。
这是怎么回事。 保存元框时,无论如何都是错误的......
设置字段数组的部分:
// Create the fields array
$prefix = 'dsmeta_';
$custom_meta_fields = array(
array(
'label' => 'Image',
'desc' => '',
'id' => $prefix . 'image',
'type' => 'image'
),
array(
'label' => 'Image caption',
'desc' => '',
'id' => $prefix . 'image_caption',
'type' => 'text'
)
);
部分保存功能:
add_action('save_post', 'dsslider_manager_save_extras');
function dsslider_manager_save_extras($post_id) {
global $custom_meta_fields;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// loop through fields and save the data
foreach ($custom_meta_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);
}
} // end foreach
}
这里我添加了字段的元框:
// Add meta box support
// This registers a function to be called when the WordPress admin interface is visited
add_action("admin_init", "dsslider_manager_add_meta");
function dsslider_manager_add_meta(){
// Create this cool new meta box for Portfolio Options
add_meta_box("dsslider-meta", "Brandbox Options", "dsslider_manager_meta_options", "brandbox-slider", "normal", "high");
}
这是构建元字段的功能:
function dsslider_manager_meta_options(){
global $custom_meta_fields, $post;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
// (integer) (optional) The post ID whose custom fields will be retrieved.
// Default: Current post
return $post_id;
?>
<div class="dsslider_manager_extras">
<div class="ds-metabox" data-max_rows="5" data-min_rows="0">
<table class="meta ds-input-table">
<?php
foreach ($custom_meta_fields as $field) {
$custom = get_post_meta($post->ID, $field['id'], true); // Returns a multidimensional array with all custom fields of a particular post or page.
// Past HTML markup
?>
<tbody class="ui-sortable">
<?php
echo '<tr class="row">';
echo '<td class="order"></td>';
echo '<td>';
switch($field['type']) {
// case items will go here
// image
case 'image':
$image = get_template_directory_uri().'/images/image.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if($custom) {
$image = wp_get_attachment_image_src($custom, 'thumbnail');
$image = $image[0];
} // end if statement
echo '<img src="' . $image . '" class="custom_preview_image" alt="" />
<input type="button" class="button add-image" name="' . $field['id'] . '" value="' . $custom . '"><a href="#" class="remove-image">Remove Image</a>';
break;
// text
case 'text':
echo '<input type="text" class="text" name="' . $field['id'] . '" value="' . $custom . '">';
break;
} //end switch
echo '</td>';
echo '</tr>';
} // End foreach loop
?>
</tbody>
</table><!-- End .meta ds-input-table -->
<ul class="ds-repeater-footer hl clearfix">
<li class="right">
<a href="#" class="repeatable-add ds-button">Add New Slide</a>
</li>
</ul><!-- End ul.hl clearfix repeater-footer -->
</div><!-- End .ds-metabox -->
</div><!-- End .dsslider_manager_extras -->
<?php
}
答案 0 :(得分:3)
问题是你使用$ custom_meta_fields数组生成输入字段并使用免费的密钥名称从$ _POST数组中获取信息。
这通常不会成为问题,但事实是您正在使用的某些字段实际上并未将信息传递给$ _POST数组。一个例子:
case 'image':
$image = get_template_directory_uri().'/images/image.png';
echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
if($custom) {
$image = wp_get_attachment_image_src($custom, 'thumbnail');
$image = $image[0];
} // end if statement
echo '<img src="' . $image . '" class="custom_preview_image" alt="" />
<input type="button" class="button add-image" name="' . $field['id'] . '" value="' . $custom . '"><a href="#" class="remove-image">Remove Image</a>';
break;
//Later on....
foreach ($custom_meta_fields as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']]; //<-- BOOM
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
在那个foreach循环中,你试图获取一个不存在的变量$ _POST ['dsmeta_image'],因为你的表单永远不会传递那个特定的键。一个简单的解决方案是这样的:
foreach ($custom_meta_fields as $field) {
if(isset($_POST[$field['id'])){
$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);
}
}
else
continue;
} // end foreach
您还需要记住Button类型的输入字段不要向$ _POST数组发送信息。如果这是您的意图,您需要通过隐藏字段或其他内容发送您想要的信息。
希望这有帮助。