Search code examples
wordpresspluginscmb2

CMB2 How do I get my custom fields to show in my template?


<table class="lg-directory-table">

<?php foreach( $grouped_results as $key => $grouped_result ): ?>
    
    <?php if( !empty( $group_posts ) ): ?>
    <tr>
        <th colspan="<?php echo count( $args['fields'] ); ?>" scope="rowgroup">
            <?php $term = get_term($key, LG_PREFIX . 'directory_group'); ?>
            <?php echo $term->name; ?>
        </th>
    </tr>
    <?php endif; ?>
    
    <?php if( 
        !isset( $args['template_options']['show_headers'] )
        || $args['template_options']['show_headers'] 
    ): ?>
    <tr>
        <?php foreach( $args['template_options']['fields'] as $field_name ): ?>
            <th><small><?php echo apply_filters('lg_directory_member_field_header', $field_name ); ?></small></th>
        <?php endforeach; ?>
    </tr>
    <?php endif; ?>
    
    <?php global $post; ?>
    <?php foreach( $grouped_result as $post ): setup_postdata($post); ?>
        
        <?php
            
            $member = array();
            $member_metabox = cmb2_get_metabox( LG_PREFIX . 'directory_member' , LG_PREFIX . 'directory_member' );
            
            foreach( $member_metabox->prop( 'fields' ) as $field_id => $field ) {
                
                $field_value = get_post_meta( $post->ID, $field_id, true );
                
                if( $field_value ) {
                    $array_field_id = str_replace( LG_PREFIX . 'directory_member_', '', $field_id );
                    $member[ $array_field_id ] = $field_value;
                }
            }
        
        ?>
    
        <tr>
            <?php foreach( $args['template_options']['fields'] as $field_name ): ?>
            
                <td>
                    <span class="<?php echo 'lg-directory-' . $field_name; ?>">
                    <?php                           
                        $field_value = '';
                        if( isset( $member[$field_name] ) ) {
                            $field_value = $member[$field_name];
                        }
                        echo apply_filters('lg_directory_member_field_value', $field_value, $field_name, $member, $args );
                    ?>
                    </span>
                </td>
                
            <?php endforeach; ?>    
        </tr>
        
    <?php endforeach; wp_reset_postdata(); ?>
    
<?php endforeach; ?>

</table>
<?php endif; ?>

My fields show and save data in the backend, but in my template, they just refuse to show the data at all. Is there a special way I need to do it? I can't figure this thing out. If someone could just point me in the right direction. (And not to the CMB2 GitHub no one comments their code so I have no idea what they're doing and what goes where!)

Please help!


Solution

  • I will answer with example.

    This is my CMB2 Meta box back end part:

    /*  Tour Metabox  */
    add_action( 'cmb2_admin_init', 'lmbox_register_tour_metabox' );
    
    function lmbox_register_tour_metabox() {
    
        $prefix = 'tour_';
    
        //Itinerary Metabox
        $lmd_metabox = new_cmb2_box( array(
            'id'            => 'tour_metabox2',
            'title'         => esc_html__( 'Tour Itinerary', 'lombokmedia' ),
            'object_types'  => array( 'tour' ), // Post type
            'context'    => 'normal',
            'priority'   => 'high',
        ) );
        
        $group_field_id = $lmd_metabox->add_field( array(
            'id'          => $prefix . 'demo',
            'type'        => 'group',
            'description' => esc_html__( 'Rincian jadwal tour', 'cmb2' ),
            'options'     => array(
                'group_title'   => esc_html__( 'Hari {#}', 'cmb2' ),
                'add_button'    => esc_html__( 'Tambah', 'cmb2' ),
                'remove_button' => esc_html__( 'Hapus', 'cmb2' ),
                'sortable'      => true,
                // 'closed'     => true,
            ),
        ) );
        
        $lmd_metabox->add_group_field( $group_field_id, array(
            'name'       => esc_html__( 'Judul', 'cmb2' ),
            'id'         => 'titlex1',
            'type'       => 'text',
            'default'   => 'Hari ',
        ) );
    
        $lmd_metabox->add_group_field( $group_field_id, array(
            'name'        => esc_html__( 'Rincian', 'cmb2' ),
            'description' => esc_html__( 'Rincian itinerary hari itu', 'cmb2' ),
            'id'          => 'itinerary_desc',
            'type'        => 'wysiwyg',
            'options' => array(
                'wpautop' => true, // use wpautop?
                'media_buttons' => false,
                'textarea_rows' => get_option('default_post_edit_rows', 5),
            ),
        ) );
    
    } //END
    

    And this is how I displayed it in single-tour.php (just the itinerary part)

    <h2 class="post-title"><?php _e('Jadwal Kegiatan', 'lombokmedia') ?> <a href="<?php the_permalink(); ?>" title="Continue reading <?php the_title(); ?>"><?php the_title(); ?></a>:</h2>
    
    <?php
    $entries = get_post_meta( get_the_ID(), 'tour_demo', true );
    
    foreach ( (array) $entries as $key => $entry ) {
    
        $title = $desc = '';
    
        if ( isset( $entry['titlex1'] ) ) {
            $title = esc_html( $entry['titlex1'] );
        }
    
        if ( isset( $entry['itinerary_desc'] ) ) {
            $desc = wpautop( $entry['itinerary_desc'] );
        }
    
        echo '<div class="tour-item">';
        echo '<h3>'.$title.'</h3>';
        echo '<div>'.$desc.'</div>';
        echo '</div>';
    } 
    ?>
    

    I hope this help you understand and figured it out.