如何将多个$META_BOX显示到单独的表中

时间:2011-07-12 作者:mic3000

我正在类中编写插件,并创建了$meta\\u框,我可以检索所有字段,但它们是重复的,并且在同一个表中。

这是我添加$meta\\u框的代码

function ri_add_custom_box() {
    global $prefix, $meta_box, $meta_boxes;


$prefix = \'_ri_custom_meta_\'; // a custom prefix to help us avoid pulling data from the wrong meta box
$meta_boxes = array();

$meta_boxes[] = array(  
\'id\' => \'ri_meta_box_panel1\', // the id of our meta box
\'title\' => \'Test1\', // the title of the meta box
\'page\' => \'ri_my_page\', // display this meta box on post editing screens
\'context\' => \'side\',
\'priority\' => \'high\', // keep it near the top
    \'fields\' => array( // all of the options inside of our meta box
        array(
            \'name\' => \'Test1:\',
            \'desc\' => \'Test1\',
            \'id\' => $prefix . \'Test1\',
            \'type\' => \'text\',
            \'std\' => \'\'
        ),
    )
);

$meta_boxes[] = array(  
\'id\' => \'ri_meta_box_panel2\', // the id of our meta box
\'title\' => \'Test2\', // the title of the meta box
\'page\' => \'ri_my_page\', // display this meta box on post editing screens
\'context\' => \'side\',
\'priority\' => \'high\', // keep it near the top
    \'fields\' => array( // all of the options inside of our meta box
        array(
            \'name\' => \'Test2:\',
            \'desc\' => \'Test2\',
            \'id\' => $prefix . \'Test2\',
            \'type\' => \'text\',
            \'std\' => \'\'
        ),
    )
);

foreach ($meta_boxes as $meta_box) {
    new Ri($meta_box);
    add_meta_box($meta_box[\'id\'], $meta_box[\'title\'], array(&$this, \'ri_display_metaboxes\'), $meta_box[\'page\'], $meta_box[\'context\'], $meta_box[\'priority\']);
}
}
这是我显示$meta\\u框的代码

function ri_display_metaboxes() {
    global $meta_box, $meta_boxes, $post;

    echo \'<input type="hidden" name="ri_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)), \'" />\';
    echo \'<table class="form-table">\';

        foreach ($meta_boxes as $meta_box) {
            foreach ($meta_box[\'fields\'] as $field) {

            $meta = get_post_meta($post->ID, $field[\'id\'], true);

    echo \'<tr>\', // create a table row for each option
                \'<th style="width:20%"><label for="\', $field[\'id\'], \'">\', $field[\'name\'], \'</label></th>\',
                \'<td>\';
        switch ($field[\'type\']) {

            case \'text\': // the HTML to display for type=text options
                echo \'<input type="text" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" value="\', $meta ? $meta : $field[\'std\'], \'" size="30" style="width:97%" />\', \'
\', $field[\'desc\'];
                break;     

            case \'textarea\': // the HTML to display for type=textarea options
                echo \'<textarea name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" cols="60" rows="8" style="width:97%">\', $meta ? $meta : $field[\'std\'], \'</textarea>\', \'
\', $field[\'desc\'];
                break;

            case \'select\': // the HTML to display for type=select options
                echo \'<select name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'">\';
                foreach ($field[\'options\'] as $option) {
                    echo \'<option\', $meta == $option ? \' selected="selected"\' : \'\', \'>\', $option, \'</option>\';
                }
                echo \'</select>\';
                break;

            case \'radio\': // the HTML to display for type=radio options
                foreach ($field[\'options\'] as $option) {
                    echo \'<input type="radio" name="\', $field[\'id\'], \'" value="\', $option[\'value\'], \'"\', $meta == $option[\'value\'] ? \' checked="checked"\' : \'\', \' />\', $option[\'name\'];
                }
                break;

            case \'checkbox\': // the HTML to display for type=checkbox options
                echo \'<input type="checkbox" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'"\', $meta ? \' checked="checked"\' : \'\', \' />\';
                break;

        }
        echo     \'<td>\',
            \'</tr>\';
                }
            }
        echo \'</table>\';
}
我想在第一个metabox Test1和第二个metabox Test2中显示,如此Metabox1test1

Metabox2Test2

但现在有了这个密码

Metabox1Test1Test2

Metabox2Test1Test2

对不起,我的英语不好。感谢您的帮助!

3 个回复
SO网友:Bainternet

问题在于ri_display_metaboxes() 更具体地说,这一行:

foreach ($meta_boxes as $meta_box) {
它告诉WordPress在每个元盒显示回调上呈现两个/所有元盒。

要克服它,您可以使用$callback_args 的参数add_meta_box 作用

所以试试这个:

function ri_add_custom_box() {
    global $prefix, $meta_box, $meta_boxes;


    $prefix = \'_ri_custom_meta_\'; // a custom prefix to help us avoid pulling data from the wrong meta box
    $meta_boxes = array();

    $meta_boxes[] = array(  
    \'id\' => \'ri_meta_box_panel1\', // the id of our meta box
    \'title\' => \'Test1\', // the title of the meta box
    \'page\' => \'ri_my_page\', // display this meta box on post editing screens
    \'context\' => \'side\',
    \'priority\' => \'high\', // keep it near the top
        \'fields\' => array( // all of the options inside of our meta box
            array(
                \'name\' => \'Test1:\',
                \'desc\' => \'Test1\',
                \'id\' => $prefix . \'Test1\',
                \'type\' => \'text\',
                \'std\' => \'\'
            ),
        )
    );

    $meta_boxes[] = array(  
    \'id\' => \'ri_meta_box_panel2\', // the id of our meta box
    \'title\' => \'Test2\', // the title of the meta box
    \'page\' => \'ri_my_page\', // display this meta box on post editing screens
    \'context\' => \'side\',
    \'priority\' => \'high\', // keep it near the top
        \'fields\' => array( // all of the options inside of our meta box
            array(
                \'name\' => \'Test2:\',
                \'desc\' => \'Test2\',
                \'id\' => $prefix . \'Test2\',
                \'type\' => \'text\',
                \'std\' => \'\'
            ),
        )
    );

    foreach ($meta_boxes as $meta_box) {
        new Ri($meta_box);
        add_meta_box($meta_box[\'id\'], $meta_box[\'title\'], array(&$this, \'ri_display_metaboxes\'), $meta_box[\'page\'], $meta_box[\'context\'], $meta_box[\'priority\'],array(\'fields\' => $meta_box[\'fields\']));
    }
}


function ri_display_metaboxes($post,$meta_b) {
    global $meta_boxes, $post;

    echo \'<input type="hidden" name="ri_meta_box_nonce" value="\', wp_create_nonce(basename(__FILE__)), \'" />\';
    echo \'<table class="form-table">\';

        foreach ((array)$meta_b[\'arg\'][\'fields\'] as $field) {
            $meta = get_post_meta($post->ID, $field[\'id\'], true);
            echo \'<tr>\', // create a table row for each option
                \'<th style="width:20%"><label for="\', $field[\'id\'], \'">\', $field[\'name\'], \'</label></th>\',
                \'<td>\';
            switch ($field[\'type\']) {
                case \'text\': // the HTML to display for type=text options
                    echo \'<input type="text" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" value="\', $meta ? $meta : $field[\'std\'], \'" size="30" style="width:97%" />\', \'\', $field[\'desc\'];
                    break;     
                case \'textarea\': // the HTML to display for type=textarea options
                    echo \'<textarea name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'" cols="60" rows="8" style="width:97%">\', $meta ? $meta : $field[\'std\'], \'</textarea>\', \'\', $field[\'desc\'];
                    break;
                case \'select\': // the HTML to display for type=select options
                    echo \'<select name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'">\';
                    foreach ($field[\'options\'] as $option) {
                        echo \'<option\', $meta == $option ? \' selected="selected"\' : \'\', \'>\', $option, \'</option>\';
                    }
                    echo \'</select>\';
                    break;
                case \'radio\': // the HTML to display for type=radio options
                    foreach ($field[\'options\'] as $option) {
                        echo \'<input type="radio" name="\', $field[\'id\'], \'" value="\', $option[\'value\'], \'"\', $meta == $option[\'value\'] ? \' checked="checked"\' : \'\', \' />\', $option[\'name\'];
                    }
                    break;
                case \'checkbox\': // the HTML to display for type=checkbox options
                    echo \'<input type="checkbox" name="\', $field[\'id\'], \'" id="\', $field[\'id\'], \'"\', $meta ? \' checked="checked"\' : \'\', \' />\';
                    break;
            }
            echo     \'<td>\',
                \'</tr>\';
        }
    echo \'</table>\';
}

SO网友:mic3000

我发现了错误!

foreach ((array)$meta_b[\'args\'][\'fields\'] as $field) {
而不是

foreach ((array)$meta_b[\'arg\'][\'fields\'] as $field) {

SO网友:mic3000

现在如何保存post数据?

这是我的代码:

function ri_meta_save_data($post_id) {
    global $post;

    // check autosave
    if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // verify nonce -- checks that the user has access
    if (!wp_verify_nonce($_POST[\'ri_meta_box_nonce\'], basename(__FILE__))) {
        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;
    }

    foreach ((array)$meta_b[\'args\'][\'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);
        }
    }
}
提前感谢!

结束

相关推荐

后端中的分类下拉Metabox

我创建了名为Brands 并将其分级,以便我可以在其中添加汽车品牌和车型,并保持它们之间的关系,如下所示:福特(Ford)野马(Mustang)蒙迪欧(Mondeo)的问题是,这个列表可能很长,每个帖子只需要一个品牌和一个车型,所以复选框会产生误导。我正在考虑将metabox拆分为两个(一个用于品牌,一个用于模型),并将其下拉列表。因此,当在第一个下拉列表中选择品牌时,第二个下拉列表将仅显示与该品牌相关的型号。但我不知道如何编码。也许有人能给我举个例子?