我正在使用Wordpress建立一个网站,在那里我想要显示来自自定义字段的元数据。我已经在我的function.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',
) );
}好的,这是在post部分的工作,这是工作。但是当我尝试在前端显示元数据时
<?php
$text = get_post_meta( get_the_ID(), '_yourprefix_text', true );
echo esc_html( $text );
?>没有任何回声。
任何人请找出问题所在。
发布于 2020-09-15 19:58:05
看起来你只是打错了。id与'yourprefix_text‘和'_yourprefix_text’的meta_key不匹配
已修复:
<?php
$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );
echo esc_html( $text );
?>https://stackoverflow.com/questions/63780310
复制相似问题