• I’m no programmer so I’m clueless on solutions.
    I have been using CMB2
    for a Portfolio/Project custom post type.

    I’ve incorporated a slideshow that uses Group Field metadata for each slide.

    METABOX//

    // $group_field_id is the field id string, so in this case: $prefix . 'demo'
       $group_field_id = $cmb_group->add_field( array(
        'id'          => $prefix . 'demo',
        'type'        => 'group',
        'options'     => array(
        'group_title'   => __( 'Image {#}', 'cmb2' ), // {#} gets replaced by row number
            'add_button'    => __( 'Add Another Image', 'cmb2' ),
            'remove_button' => __( 'Remove Image', 'cmb2' ),
            'sortable'      => true, // beta
            'closed'     => true, // true to have the groups closed by default
        ),
      ) );

    Followed this to display meta data for those group fields.
    Everything works perfectly fine when I use this chunk of code:

    FRONT-END//

    <div>
     <ul class="slides">
    
                <?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
    
                    foreach ( (array) $entries as $key => $entry ) {
    
                        $img = $img_url = $caption = '';
                    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'] ) : '';
                        echo '<li data-thumb="'. $img_url .'">';
                        echo $img;
                        echo $caption;
                        echo '</li>';
    
                } ?>
      </ul>
      </div>

    but I would very much like to display the .flexslider container + metadata ONLY when data exist. If fields are empty then I would like to display default text or better yet remove the whole div itself.
    I tried my best to do research but I can’t seem to figure out what is wrong.
    I’ve also tried this chunk of code as well:

    ATTEMPT//

    <?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
    if(empty ($entry)) { echo ''; }
    else {
       foreach ( (array) $entries as $key => $entry ) {
         echo '<div>';
         echo '<ul class="slides">';
       $img = $img_url = $caption = '';
    
       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'] ) : '';
        echo '<li data-thumb="'. $img_url .'">';
        echo $img;
        echo $caption;
        echo '</li>';
        echo '</ul>';
        echo '</div>';
        }
      }
     ?>

    The only good thing about the code above is that it definitely removes the div when metafield is empty but if the metadata DOES exist the div is still gone.

    I’d basically like to figure out how I can display the first chunk of code ONLY if images were uploaded to Group Field and if not then display nothing at all not even the container div.

    Can anyone please explain where I went wrong?

  • The topic ‘CMB2 Display group field meta data if exists, if empty display default text’ is closed to new replies.