gpt4 book ai didi

wordpress - Metabox 保存功能貌似不行

转载 作者:行者123 更新时间:2023-12-04 05:17:40 27 4
gpt4 key购买 nike

我目前正在创建元框。我使用了以下教程和一些自适应。教程链接:http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/

现在我收到以下错误消息:

Notice: Undefined index: dsmeta_image in /customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php on line 181 Notice: Undefined index: dsmeta_image_caption in /customers/0/d/a/xxx/httpd.www/wordpress/wp-content/plugins/ds-flexslider/includes/cpt-manager.php on line 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
}

最佳答案

问题是您使用 $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 数组。如果这是您的意图,您需要通过隐藏字段或其他方式发送您想要的信息。

希望这会有所帮助。

关于wordpress - Metabox 保存功能貌似不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14055288/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com