语法高级自定义字段在wordpress

问题描述:

我想显示一个字段,但它显示的价值很好,但标签的外部?!??!语法高级自定义字段在wordpress

echo '<h2>'. the_field('where') .'</h2>'; 

输出=

"London" 
<h2></h2> 

应该是=

<h2>London</h2> 
+0

你缺少的东西,你在H3标签显示但你想要h2标签?我不明白 –

+0

the_field函数应该返回的值不是回显它 –

使用此:

<h2><?php the_field('where'); ?></h2> 

说明:

你的代码有因为如何echo作品有输出。它首先生成整个字符串(运行函数),然后渲染输出。所以,如果功能the_field有输出,它会生成你所看到的。

基本上你的代码就相当于:

$title = '<h3>'. the_field('where') .'</h3>'; 
echo $title; 

例子:

function test() { 
    echo '1'; 
    return '2'; 
} 
echo 'PRE - ' . test() . ' - POST'; 

这里是结果:

$ php test.php 
1PRE - 2 - POST 

因为你有这样的功能:

function the_field($text){ 
echo $text; 
} 
echo '<h3>'. the_field('where') .'</h3>'; 

更改功能:

function the_field($text){ 
return $text; 
} 
echo '<h3>'. the_field('where') .'</h3>'; 

为什么?因为PHP在打印输出回显之前执行该功能。

+0

或他与h2标签函数中的回声,并尝试用h3再次exho? –