2015-03-11 14 views
1

Я использую Custom Metaboxes 2, и я не могу получить повторяющиеся значения групп для отображения на лицевой стороне. У меня есть повторяющаяся группа с одним текстовым полем.CMB2 Repeatable Group Display

I used this as a guide.

Сначала у меня есть код бэкэнд, а затем код переднего конца.

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', 
)); 

} 

FRONT END:

$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. 
} 

ответ

5

ли вы $prefix определены в вашем переднем конце кода? Это единственное, что я вижу, что там не так. Если $prefix не определен, вы просматриваете post_meta с ключом songs вместо _awc_songs.

Смежные вопросы