我目前正在创建元框。我用了下面的教程和一些自适应的。教程链接:http ://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/
现在我收到以下错误消息:
注意:未定义索引:第 181 行 /customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php 中的 dsmeta_image 注意:未定义索引:dsmeta_image_caption在第 181 行的 /customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php
似乎该变量不存在,如果我理解正确,我正在使用 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
}