gpt4 book ai didi

php - CMB2 可重复组显示

转载 作者:行者123 更新时间:2023-12-02 01:00:38 25 4
gpt4 key购买 nike

我正在使用 Custom Metaboxes 2,但我无法在前端显示可重复的组值。我有一个带有单个文本字段的可重复组。

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

}

前端:

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

最佳答案

您的前端代码中是否定义了$prefix?这是我唯一能看到的错误。如果未定义 $prefix,您将查找带有键 songs 而不是 _awc_songs 的 post_meta。

关于php - CMB2 可重复组显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28993041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com