首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CMB2可重复组显示

CMB2可重复组显示
EN

Stack Overflow用户
提问于 2015-03-11 17:11:32
回答 1查看 6.6K关注 0票数 2

我使用自定义Metaboxes 2,我无法获得可重复的组值显示在前端。我有一个可重复的组,只有一个文本字段。

我用这个当向导

我先有后端代码,然后是前端代码。

代码语言:javascript
复制
add_action( 'cmb2_init', 'awc_register_repeatable_group_field_metabox' );

function awc_register_repeatable_group_field_metabox() {

    // Start with an underscore to hide fields from custom fields list
    $prefix = '_awc_';

/**
 * Repeatable Field Groups
 */
$cmb_group = new_cmb2_box( array(
    'id'           => $prefix . 'songs',
    'title'        => __( 'Track Listing', 'cmb2' ),
    'object_types' => array( 'awc_discography', ),
) );

// $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',
    //'description' => __( 'Generates reusable form entries', 'cmb2' ),
    'options'     => array(
        'group_title'   => __( 'Track {#}', 'cmb2' ), // {#} gets replaced by row number
        'add_button'    => __( 'Add Another Track', 'cmb2' ),
        'remove_button' => __( 'Remove Track', 'cmb2' ),
        'sortable'      => true, // beta
    ),
) );

/**
 * Group fields works the same, except ids only need
 * to be unique to the group. Prefix is not needed.
 *
 * The parent field's id needs to be passed as the first argument.
 */
$cmb_group->add_group_field( $group_field_id, array(
    'name'       => __( 'Title', 'cmb2' ),
    'id'         => 'title',
    'type'       => 'text',
) );

}

前端:

代码语言:javascript
复制
$entries = get_post_meta( get_the_ID(), $prefix . 'songs', true );

foreach ( (array) $entries as $key => $entry ) {

$songtitle = '';

if ( isset( $entry['demo'] ) )
    $title = esc_html( $entry['demo'] );


// Do something with the data
echo $title; // Don't know if this is the correct method.
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-15 15:01:00

在前端代码中定义了$prefix吗?这是我唯一能看到的错误的地方。如果没有定义$prefix,则使用键songs而不是_awc_songs查找post_meta。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28993041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档