【发布时间】:2019-07-11 11:29:39
【问题描述】:
我正在尝试获取自定义字段'product_url' 的值,此代码位于functions.php 中,我在单个帖子中使用短代码。自定义字段'product_url' 存在于该帖子中且不为空。
function metavalue() {
GLOBAL $post;
$meta = get_post_meta($post->ID, 'product_url', true);
echo $meta;
}
add_shortcode('url_short', 'metavalue');
当我使用简码时没有显示任何内容。 var_dump($meta); 会输出
字符串(0) ""
。
【问题讨论】:
-
你调试过@meta在函数内不为空的代码吗?
-
不要使用
echo使用return,有时短代码会在帖子内容完成并输出之前执行时打印在奇怪的地方。或者在输出之后调用 exit,这样在页面中更容易找到它。我不确定这是否只是显示问题,但代码对我来说看起来不错。 -
我试过用 return $meta;它仍然没有输出任何东西。
-
@Jesses 我也更新了我的代码。请调试问题。确保有价值。