简体   繁体   English

CMB2 如何让我的自定义字段显示在我的模板中?

[英]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!) (而不是 CMB2 GitHub 没有人评论他们的代码,所以我不知道他们在做什么以及去哪里!)

Please help!请帮忙!

I will answer with example.我会举例回答。

This is my CMB2 Meta box back end part:这是我的CMB2 Meta box 后端部分:

/*  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)这就是我在single-tour.php中显示它的方式(只是行程部分)

<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.我希望这可以帮助您理解并弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM