Custom metabox not working

时间:2013-02-21 作者:tigre

早晨

我有个电话functions.php 在我的主题目录中包含一个自定义元框php文件,名为:/metaboxes/home-meta.php

调用代码如下:

    add_action( \'add_meta_boxes_page\',\'load_home_meta\' );
    function load_home_meta() {
        $post_id = $_GET[\'post\'] ? $_GET[\'post\'] : $_POST[\'post_ID\'] ;
        if($post_id == \'104\'){
            include( get_template_directory() . \'/metaboxes/home-meta.php\' );
        }
    }
include正在工作(有一个add_meta_boxes 函数包装在home-meta.php 下面的文件(更新代码))。

然而,我在第一次设置每个元数据库时都会出错caseswitch 用于回调函数。错误示例如下:

警告:为/Users/[my\\u mac]/Sites/[site\\u dir]/wp content/themes/[site\\u themes]/metaboxes/home meta中的foreach()提供的参数无效。php在线224

这个home-meta.php 代码为:

    <?php

    /**
     * Home Page Custom Meta Content
     *
    **/

add_meta_box(  
    \'home_meta_box\', // $id  
    \'Home Page Content\', // $title   
    \'show_home_meta_box\', // $callback  
    \'page\', // $page  
    \'normal\', // $context  
    \'high\'); // $priority
add_meta_box(  
    \'home_meta_box_lower_1\', // $id  
    \'Home Lower Left\', // $title   
    \'show_home_meta_box_lower_left\', // $callback  
    \'page\', // $page  
    \'normal\', // $context  
    \'high\'); // $priority    
add_meta_box(  
    \'home_meta_box_lower_2\', // $id  
    \'Home Lower Center\', // $title   
    \'show_home_meta_box_lower_center\', // $callback  
    \'page\', // $page  
    \'normal\', // $context  
    \'high\'); // $priority 
add_meta_box(  
    \'home_meta_box_lower_3\', // $id  
    \'Home Lower Right\', // $title   
    \'show_home_meta_box_lower_right\', // $callback  
    \'page\', // $page  
    \'normal\', // $context  
    \'high\'); // $priority 


    // Creating Array for Fields
    $prefix = \'home_\';
    $home_meta_fields = array(
        array(
            \'label\' => \'Caption Title\',
            \'desc\' => \'Upper section H2 caption title.\',
            \'id\' => $prefix.\'title\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Caption Sub Title\',
            \'desc\' => \'Upper section H3 caption title.\',
            \'id\' => $prefix.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Caption\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(  
            \'label\'  => \'Caption Image\',  
            \'desc\'  => \'Upload a pre-cropped 1140px wide x 530px tall web-optimized image.\',  
            \'id\'    => $prefix.\'image\',  
            \'type\'  => \'image\'  
        )
    );// end caption array

    $prefix2 = \'home_lower_left_\';
    $home_meta_fields_lower_left = array(
        array(
            \'label\' => \'Column Title\',
            \'desc\' => \'H2 title for column.\',
            \'id\' => $prefix2.\'title\',
            \'type\' => \'text\'
        ),
        array(  
            \'label\'  => \'Column Image\',  
            \'desc\'  => \'Upload a pre-cropped 360px wide x 300px tall web-optimized image.\',  
            \'id\'    => $prefix2.\'image\',  
            \'type\'  => \'image\'  
        ),
        array(
            \'label\' => \'Column Sub-Title\',
            \'desc\' => \'H3 sub-title above text block.\',
            \'id\' => $prefix2.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Text block\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix2.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(
            \'label\' => \'Button Label\',
            \'desc\' => \'Button link label (what user reads on button).\',
            \'id\' => $prefix2.\'btn_label\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button Title Tag\',
            \'desc\' => \'On hover and SEO text.\',
            \'id\' => $prefix2.\'btn_title_tag\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button URL\',
            \'desc\' => \'URL where button links to. Enter http:// to work.\',
            \'id\' => $prefix2.\'btn_url\',
            \'type\' => \'text\'
        )
    );// end lower left array

    $prefix3 = \'home_lower_center_\';
    $home_meta_fields_lower_center = array(
        array(
            \'label\' => \'Column Title\',
            \'desc\' => \'H2 title for column.\',
            \'id\' => $prefix3.\'title\',
            \'type\' => \'text\'
        ),
        array(  
            \'label\'  => \'Column Image\',  
            \'desc\'  => \'Upload a pre-cropped 360px wide x 300px tall web-optimized image.\',  
            \'id\'    => $prefix3.\'image\',  
            \'type\'  => \'image\'  
        ),
        array(
            \'label\' => \'Column Sub-Title\',
            \'desc\' => \'H3 sub-title above text block.\',
            \'id\' => $prefix3.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Text block\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix3.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(
            \'label\' => \'Button Label\',
            \'desc\' => \'Button link label (what user reads on button).\',
            \'id\' => $prefix3.\'btn_label\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button Title Tag\',
            \'desc\' => \'On hover and SEO text.\',
            \'id\' => $prefix3.\'btn_title_tag\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button URL\',
            \'desc\' => \'URL where button links to. Enter http:// to work.\',
            \'id\' => $prefix3.\'btn_url\',
            \'type\' => \'text\'
        )
    );// end lower center array

    $prefix4 = \'home_lower_right_\';
    $home_meta_fields_lower_right = array(
        array(
            \'label\' => \'Column Title\',
            \'desc\' => \'H2 title for column.\',
            \'id\' => $prefix4.\'title\',
            \'type\' => \'text\'
        ),
        array(  
            \'label\'  => \'Column Image\',  
            \'desc\'  => \'Upload a pre-cropped 360px wide x 300px tall web-optimized image.\',  
            \'id\'    => $prefix4.\'image\',  
            \'type\'  => \'image\'  
        ),
        array(
            \'label\' => \'Column Sub-Title\',
            \'desc\' => \'H3 sub-title above text block.\',
            \'id\' => $prefix4.\'sub_title\',
            \'type\' => \'text\'
        ),
      array(  
          \'label\'=> \'Text block\',  
          \'desc\'  => \'Caption text block.\',  
          \'id\'    => $prefix4.\'caption\',  
          \'type\'  => \'textarea\'  
      ),
        array(
            \'label\' => \'Button Label\',
            \'desc\' => \'Button link label (what user reads on button).\',
            \'id\' => $prefix4.\'btn_label\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button Title Tag\',
            \'desc\' => \'On hover and SEO text.\',
            \'id\' => $prefix4.\'btn_title_tag\',
            \'type\' => \'text\'
        ),
        array(
            \'label\' => \'Button URL\',
            \'desc\' => \'URL where button links to. Enter http:// to work.\',
            \'id\' => $prefix4.\'btn_url\',
            \'type\' => \'text\'
        )
    );// end lower right array


    //The Callback
    function show_home_meta_box() {
    global $home_meta_fields, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }

    // Save the Data
    function save_home_meta($post_id) {  
        global $home_meta_fields;  

        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_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;  
        }  

        // loop through fields and save the data  
        foreach ($home_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_action(\'save_post\', \'save_home_meta\');
    // end Upper Home Content

    // Lower Left
    //The Callback
    function show_home_meta_box_lower_left() {
    global $home_meta_fields_lower_left, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_left_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields_lower_left as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }

    // Save the Data
    function save_home_meta_lower_left($post_id) {  
        global $home_meta_fields_lower_left;  

        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_meta_box_left_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;  
        }  

        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_left 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_action(\'save_post\', \'save_home_meta_lower_left\');
    // end lower left

    // Lower Center Column
    //The Callback
    function show_home_meta_box_lower_center() {
    global $home_meta_fields_lower_center, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_center_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields_lower_center as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }

    // Save the Data
    function save_home_meta_lower_center($post_id) {  
        global $home_meta_fields_lower_center;  

        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_meta_box_center_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;  
        }  

        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_center 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_action(\'save_post\', \'save_home_meta_lower_center\');
    // end lower center

    // Lower Right Column
    //The Callback
    function show_home_meta_box_lower_right() {
    global $home_meta_fields_lower_right, $post;
    // Using nonce for verification
    echo \'<input type="hidden" name="home_meta_box_right_nonce" value="\'.wp_create_nonce(basename(__FILE__)).\'" />\';

        //Begin field table and loop
        echo \'<table class="form-table">\';
        foreach ($home_meta_fields_lower_right as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            // begin a table row with
            echo \'<tr>
                            <th><label for="\'.$field[\'id\'].\'">\'.$field[\'label\'].\'</label></th>
                            <td>\';
                            switch($field[\'type\']) {
                                // case items will go here
                                // text  
                                case \'text\':  
                                    echo \'<input type="text" name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" value="\'.$meta.\'" size="30" /> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                // textarea  
                                case \'textarea\':  
                                    echo \'<textarea name="\'.$field[\'id\'].\'" id="\'.$field[\'id\'].\'" cols="60" rows="4">\'.$meta.\'</textarea> 
                                        <br /><span class="description">\'.$field[\'desc\'].\'</span>\';  
                                break;
                                case \'image\':  
                                    $image = get_template_directory_uri().\'/library/images/img-preview-blank.png\';    
                                    echo \'<span class="custom_default_image" style="display:none">\'.$image.\'</span>\';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, \'medium\'); $image = $image[0]; }                 
                                    echo    \'<input name="\'.$field[\'id\'].\'" type="hidden" class="custom_upload_image" value="\'.$meta.\'" /> 
                                                <img src="\'.$image.\'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">\'.$field[\'desc\'].\'\';  
                                break;  
                            } // end switch
            echo \'</td></tr>\';
        } // end foreach
        echo \'</table>\'; // end table
    }

    // Save the Data
    function save_home_meta_lower_right($post_id) {  
        global $home_meta_fields_lower_right;  

        // verify nonce  
        if (!wp_verify_nonce($_POST[\'home_meta_box_right_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;  
        }  

        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_right 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_action(\'save_post\', \'save_home_meta_lower_right\');
    // end lower right

    ?>

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

问题#1:

第一个问题:内部home-meta.php, 把你所有的add_meta_box() 呼叫outsideadd_meta_boxes_home() 回调,并清除回调和add_action() 呼叫你已经处理好了add_meta_boxes_page 在中回调functions.php, 其中包括home-meta.php.

问题2:

警告:为/Users/[my\\u mac]/Sites/[site\\u dir]/wp content/themes/[site\\u themes]/metaboxes/home meta中的foreach()提供的参数无效。php在线224

假设这是第224行:

foreach ($home_meta_fields as $field) {
我认为问题是你要全球化$home_meta_fields after you define it:

$home_meta_fields = array( ... );
。。。之后是:

global $home_meta_fields, $post;
foreach ( $home_meta_fields as $field ) {
尝试全球化$home_meta_fields 在定义之前:

global $home_meta_fields;
$home_meta_fields = array( ... );
。。。然后,它应该被识别为foreach() 呼叫

结束

相关推荐

创建自定义分类并在Metabox下拉列表中显示

我想在编辑后的屏幕上创建一个下拉列表,其中包含3个已经存在的标签。最简单的方法是什么?基本上,我所要寻找的只是一个简单的下拉列表,它将以下标签之一添加到帖子本身中;\'炖牛肉、豌豆汤辣椒\'。我还希望“炖牛肉”是默认的。提前谢谢你编辑:由于我不希望用户在任何时候都能够显示这三个类别中的一个以上,并且必须选择根据自己的意愿轻松更改哪一个类别,所以标签可能不是最好的解决方案?创建自定义分类法(“食物”)会更好吗?它们基本上用于改变帖子在网站首页上的显示方式。