2

我目前正在创建元框。我用了下面的教程和一些自适应的。教程链接: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           
}
4

1 回答 1

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 数组。如果这是您的意图,您需要通过 Hidden Field 或其他方式发送您想要的信息。

希望这可以帮助。

于 2012-12-27T21:32:09.213 回答