My custom post-type
will have various number custom meta-boxes (a div
with multiple fields) in each post:
$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>';
Below are the value variables:
//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);
Saving them using the below function:
// 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');
As various posts will have different number of Metaboxes, with below code I want to count $metaBox
and display with foreach
.
<div class="wrap">
<?php
if (isset($metaBox) && is_array($metaBox)) {
$i = 1;
$metaBox = '';
foreach ($metaBox as $box) {
echo '$metaBox';
}
}
echo $metaBox;
$i++;
?>
</div>
With the above, displaying only the last saved $metaBox
, not all the saved.
How can I get all the various numbers of $metaBox
in a post?
[]
toname
did not worked; I want to get the entire multiple$metaBox
es. – theKing Jun 16 '19 at 16:18