Мой пользовательский post-type
будет иметь различное количество пользовательских мета-полей (a 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>';
Ниже приведены переменные значения:
//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
, а не все сохраненные.
Как я могу получить все различные номера $metaBox
в сообщении?