Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在每篇文章中显示多个带有不同数字的元框?_Php_Wordpress - Fatal编程技术网

Php 如何在每篇文章中显示多个带有不同数字的元框?

Php 如何在每篇文章中显示多个带有不同数字的元框?,php,wordpress,Php,Wordpress,我的自定义帖子类型在每篇帖子中都有不同数量的自定义元框(带有多个字段的div): $metaBox = '<div class="inside"> <div> <label>Title</label> <input type="text" name="title" value="' . $title . '"> </div> &l

我的自定义
帖子类型
在每篇帖子中都有不同数量的自定义元框(带有多个字段的
div
):

$metaBox = '<div class="inside">
        <div>
            <label>Title</label>
            <input type="text" name="title" value="' . $title . '">
        </div>
        <div>
            <label>Type</label>
         <input type="text" name="type" value="' . $type . '">
        </div>
        <div>
            <label>Content</label>
            <textarea name="text">' . $text . '</textarea>
        </div>
</div>';
使用以下功能保存它们:

// SAVE FIELDS DATA
function save_meta_box_data($post_id) {
// verify taxonomies meta box nonce
if (!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], basename(__FILE__))) {
    return;
}

// return if autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return;
}

// Check the user's permissions.
if (!current_user_can('edit_post', $post_id)) {
    return;
}

// store custom fields values
// Title
if (isset($_REQUEST['title'])) {
    update_post_meta($post_id, '_title', sanitize_text_field($_POST['title']));
}

// Type
if (isset($_REQUEST['type'])) {
    update_post_meta($post_id, '_type', sanitize_text_field($_POST['type']));
}

// Text
if (isset($_REQUEST['text'])) {
    update_post_meta($post_id, '_text', sanitize_text_field($_POST['text']));
}}

add_action('save_post', 'save_meta_box_data');
由于不同的帖子会有不同数量的元盒,我想用下面的代码计算
$metaBox
并用
foreach
显示

<div class="wrap">
   <?php
    if (isset($metaBox) && is_array($metaBox)) {
        $i = 1;
        $metaBox = '';

        foreach ($metaBox as $box) {
            echo '$metaBox';
        }
    }
    echo $metaBox;
    $i++;
    ?>
</div>

通过以上操作,仅显示上次保存的
$metaBox
,而不是所有保存的。
如何在一篇帖子中获取所有不同数量的
$metaBox

您的
echo$metaBox指令在循环之外。在循环内部,指令
echo'$metaBox'
将显示字符串$metaBox,而不是内容


删除循环中
“$metaBox”
周围的单引号。丢弃无用的
$i
变量。

您的
echo$metaBox指令在循环之外。在循环内部,指令
echo'$metaBox'
将显示字符串$metaBox,而不是内容


删除循环中
“$metaBox”
周围的单引号。丢弃无用的
$i
变量。

如果有多个框,则应在每个输入的名称中添加[],这将生成一个数组。否则,您将只获得保存时的最后一个框。@rebru,将
[]
添加到
名称
无效;我想获得整个多个
$metaBox
es。如果有多个框,则应在每个输入的名称中添加[],这将生成一个数组。否则,您将只获得保存时的最后一个框。@rebru,将
[]
添加到
名称
无效;我想获取整个多个
$metaBox
es。
进行了更改,但未工作。
进行了更改,但未工作。
<div class="wrap">
   <?php
    if (isset($metaBox) && is_array($metaBox)) {
        $i = 1;
        $metaBox = '';

        foreach ($metaBox as $box) {
            echo '$metaBox';
        }
    }
    echo $metaBox;
    $i++;
    ?>
</div>