我正在使用“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 = $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 循环来获取一个字段吗?