$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How to return all data from a grouped metabox(cmb2)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to return all data from a grouped metabox(cmb2)

matteradmin10PV0评论

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 Why is $output an array? You're using it as a string. – shanebp Commented Apr 25 at 18:59
  • Yes your right, it is my mistake, it should have been like $output =''; right? – user9637601 Commented Apr 25 at 21:46
Add a comment  | 

1 Answer 1

Reset to default 1

The 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:

  1. Moved return outside the loop: The return $output; statement is now after the foreach loop, so all entries are processed.
  2. 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.
  3. 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 using wpautop() and do_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 use var_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.

Post a comment

comment list (0)

  1. No comments so far