我正在使用Wordpress构建一个网站,我想在其中显示自定义字段中的元数据。我已经在函数.php中设置了cmb2,如下代码所示。。
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
function cmb2_sample_metaboxes() {
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page', ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$cmb->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'yourprefix_text',
'type' => 'text',
'show_on_cb' => 'cmb2_hide_if_no_cats',
) );
}
好的,这是在帖子部分工作,这是工作。但是当我尝试使用在前端显示元数据时
<?php
$text = get_post_meta( get_the_ID(), '_yourprefix_text', true );
echo esc_html( $text );
?>
没有回音。
任何人都请找出问题所在。
看起来你只是有一个拼写错误。id与meta_key不匹配:">yourprefix_text"与">_yourprefix-text">
固定:
<?php
$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );
echo esc_html( $text );
?>