Php 回波单个CMB2组字段
我正在使用“CMB2”wordpress插件添加自定义字段。还有一个选项可以添加组字段并向其中添加字段 现在,当您想要回显此字段时,“CMB2”的wiki说您将使用以下内容:Php 回波单个CMB2组字段,php,wordpress,foreach,cmb2,Php,Wordpress,Foreach,Cmb2,我正在使用“CMB2”wordpress插件添加自定义字段。还有一个选项可以添加组字段并向其中添加字段 现在,当您想要回显此字段时,“CMB2”的wiki说您将使用以下内容: $entries = get_post_meta( get_the_ID(), 'wiki_test_repeat_group', true ); foreach ( (array) $entries as $key => $entry ) { $img = $title = $desc = $capti
$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\u test\u repeat\u group
是组字段id,title
例如是内部字段id
我的问题是:
假设代码正常工作,那么为什么该代码不能正常工作:
echo get_post_meta( get_the_ID(), 'wiki_test_repeat_group', true )[title];
?
还有没有其他方法可以用一行代码获取一个字段,而不使用foreach循环获取一个字段?只需要[0],因此:
echo get_post_meta( get_the_ID(), 'wiki_test_repeat_group', true )[0][title];
会有用的