回声单个CMB2组字段

时间:2017-07-24 08:17:46

标签: php wordpress foreach cmb2

我使用“CMB2”wordpress插件添加自定义字段。并且可以选择添加组字段并向其添加字段。

现在,当您想要回显此字段时,“CMB2”的维基表示您将使用此字段:

$entries = get_post_meta( get_the_ID(), 'wiki_test_repeat_group', true );

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

    $img = $title = $desc = $caption = '';

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

    if ( isset( $entry['description'] ) ) {
        $desc = wpautop( $entry['description'] );
    }

    if ( isset( $entry['image_id'] ) ) {
        $img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
            'class' => 'thumb',
        ) );
    }

    $caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';

    // Do something with the data
}

其中wiki_test_repeat_group是组字段ID,title例如是内部字段ID。

我的问题是: 假设代码正在运行,为什么此代码也不起作用:

echo get_post_meta( get_the_ID(), 'wiki_test_repeat_group', true )[title];

还有另外一种方法我可以使用一个简短的一行代码获得一个字段而不使用foreach循环来获得一个字段吗?

1 个答案:

答案 0 :(得分:1)

只需要[0],所以:

echo get_post_meta( get_the_ID(), 'wiki_test_repeat_group', true )[0][title];

会工作。