I have a grouped metaxbox values from cmb2, when I return it to display output, it shows only one of them, but actually it has four value stored. How could I display all grouped metabox value?
Here is my attempt:
function repeatable_cmb2() {
global $post;
$entries = get_post_meta( get_the_ID(), '_kad_repeat_group', true );
if (is_countable($entries) && count($entries) > 0) {
foreach ( (array) $entries as $key => $entry ) {
$img = $img_url = $caption = $description = $layout = $title = $wysiwyg = '';
$output = array();
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
$description = isset( $entry['description'] ) ? wpautop( $entry['description'] ) : '';
$wysiwyg = isset( $entry['_kad_test_wysiwyg'] ) ? do_shortcode( $entry['_kad_test_wysiwyg'] ) : '';
$layout = isset( $entry['_kad_ppost_layout'] ) ? esc_attr($entry['_kad_ppost_layout']) : '';
$title = isset( $entry['_kad_title_ekstra'] ) ? esc_attr($entry['_kad_title_ekstra']) : '';
$output = '<h4 class="listHeading"><i class="fa '.$layout.'"></i>'.$title.'</h4>';
/*eger ikon gosterilmiyorsa, yaziyi admin panelde yenile. ikon secilmemis olabilir.*/
$output .= '<div class="ngElements">';
$output .= $description;
$output .= $wysiwyg;
$output .= $img;
$output .= $caption;
$output .= '</div>';
return $output;
}
}
}
I have a grouped metaxbox values from cmb2, when I return it to display output, it shows only one of them, but actually it has four value stored. How could I display all grouped metabox value?
Here is my attempt:
function repeatable_cmb2() {
global $post;
$entries = get_post_meta( get_the_ID(), '_kad_repeat_group', true );
if (is_countable($entries) && count($entries) > 0) {
foreach ( (array) $entries as $key => $entry ) {
$img = $img_url = $caption = $description = $layout = $title = $wysiwyg = '';
$output = array();
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
$description = isset( $entry['description'] ) ? wpautop( $entry['description'] ) : '';
$wysiwyg = isset( $entry['_kad_test_wysiwyg'] ) ? do_shortcode( $entry['_kad_test_wysiwyg'] ) : '';
$layout = isset( $entry['_kad_ppost_layout'] ) ? esc_attr($entry['_kad_ppost_layout']) : '';
$title = isset( $entry['_kad_title_ekstra'] ) ? esc_attr($entry['_kad_title_ekstra']) : '';
$output = '<h4 class="listHeading"><i class="fa '.$layout.'"></i>'.$title.'</h4>';
/*eger ikon gosterilmiyorsa, yaziyi admin panelde yenile. ikon secilmemis olabilir.*/
$output .= '<div class="ngElements">';
$output .= $description;
$output .= $wysiwyg;
$output .= $img;
$output .= $caption;
$output .= '</div>';
return $output;
}
}
}
Share
Improve this question
edited Apr 25 at 18:58
shanebp
5,0857 gold badges28 silver badges40 bronze badges
asked Apr 25 at 16:21
user9637601user9637601
52 bronze badges
2
|
1 Answer
Reset to default 1The issue in your code is that the return $output;
statement is inside the foreach
loop, causing the function to return only the first iteration's output and stop. To display all grouped metabox values, you need to accumulate the output for all entries and return it after the loop completes.
Here's the corrected code:
function repeatable_cmb2() {
global $post;
$entries = get_post_meta( get_the_ID(), '_kad_repeat_group', true );
$output = ''; // Initialize an empty string to collect all outputs
if (is_countable($entries) && count($entries) > 0) {
foreach ( (array) $entries as $key => $entry ) {
$img = $img_url = $caption = $description = $layout = $title = $wysiwyg = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array( 'class' => 'thumb' ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
$description = isset( $entry['description'] ) ? wpautop( $entry['description'] ) : '';
$wysiwyg = isset( $entry['_kad_test_wysiwyg'] ) ? do_shortcode( $entry['_kad_test_wysiwyg'] ) : '';
$layout = isset( $entry['_kad_ppost_layout'] ) ? esc_attr( $entry['_kad_ppost_layout'] ) : '';
$title = isset( $entry['_kad_title_ekstra'] ) ? esc_attr( $entry['_kad_title_ekstra'] ) : '';
// Append each entry's output to the $output string
$output .= '<h4 class="listHeading"><i class="fa ' . $layout . '"></i>' . $title . '</h4>';
$output .= '<div class="ngElements">';
$output .= $description;
$output .= $wysiwyg;
$output .= $img;
$output .= $caption;
$output .= '</div>';
}
}
return $output; // Return the complete output after the loop
}
Key Changes:
- Moved
return
outside the loop: Thereturn $output;
statement is now after theforeach
loop, so all entries are processed. - Accumulate output: The
$output
variable is initialized as an empty string before the loop and appended with each entry's HTML using.=
inside the loop. - Removed unused
$output
array: You were initializing$output
as an array but overwriting it as a string. Since you're building a string, it's cleaner to initialize it as a string from the start.
Additional Notes:
- Check for empty entries: The condition
if (is_countable($entries) && count($entries) > 0)
ensures the loop only runs if there are valid entries. This is good practice. - Sanitization: You're using
esc_attr()
for$layout
and$title
, which is good for attributes. Ensure all other outputs ($description
,$wysiwyg
,$caption
) are safe. Since you're usingwpautop()
anddo_shortcode()
, they should generally be safe, but double-check if user input could include malicious code. - Debugging: If you still don't see all values, verify the data stored in the
_kad_repeat_group
meta key. You can usevar_dump($entries);
before the loop to inspect the structure and confirm all four values are present. - Icon fallback: Your comment suggests an icon might not be selected. You could add a fallback for
$layout
to avoid empty<i>
tags, e.g.,if (empty($layout)) $layout = 'fa-default-icon';
.
Example Usage:
To display the output in a template, call the function like this:
echo repeatable_cmb2();
This should now display all grouped metabox values (all four entries) instead of just one. If you still face issues, share the structure of $entries
(e.g., via var_dump
) or any error messages for further assistance.
$output
an array? You're using it as a string. – shanebp Commented Apr 25 at 18:59$output ='';
right? – user9637601 Commented Apr 25 at 21:46