php - 显示元数据在 cmb2 的前端未显示任何内容
问题描述
我正在使用 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',
) );
}
好的,这在帖子部分有效,有效。但是当我尝试在前端显示元数据时
<?php
$text = get_post_meta( get_the_ID(), '_yourprefix_text', true );
echo esc_html( $text );
?>
没有任何回应。
任何人都请找出问题所在。
解决方案
看起来你刚刚打错了。id 与 meta_key 不匹配:' yourprefix_text ' vs ' _yourprefix_text '
固定的:
<?php
$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );
echo esc_html( $text );
?>
推荐阅读
- ubuntu - 为什么带有 php7.4-fpm 的 Nginx 没有输出?
- ssis - 在 SQL Server 中运行但在 Visual Studio 中运行时出现 SSIS 错误
- prolog - Prolog 中的切割和失败语义
- asp.net-core - Azure DevOps 将不带 IIS 的 dotnet 核心包部署到部署组
- sql - varchar 列上的 SQL Pivot
- java - 在 RecycleView 中引用的空对象上的 setLayoutManager
- javascript - 以独特的组合折腾玩家
- python-3.x - 谁能解释一下'def displayRMagicSquare(magic_square)'到底要求什么?
- makefile - makefile rule to update a variable number of targets with a batch compile
- spreadsheet - 用于复制特定单元格值的 Google 脚本