我的自定义post-type在每个帖子中都会有不同数量的自定义元框(具有多个字段的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>';以下是value变量:
//Title
$title = get_post_meta($post->ID, '_title', true);
// Type
$type = get_post_meta($post->ID, '_type', true);
// Text
$text = get_post_meta($post->ID, '_text', true);使用下面的函数保存它们:
// 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,而不是所有保存的all。我怎样才能在一篇文章中获得所有不同数量的$metaBox?
发布于 2019-06-16 23:54:27
您的echo $metaBox;指令在循环之外。在循环内部,指令echo '$metaBox';将显示字符串$metaBox,而不是内容。
删除循环内'$metaBox'两边的单引号。丢弃无用的$i变量。
https://stackoverflow.com/questions/56619731
复制相似问题