简体   繁体   English

Wordpress中高级自定义字段的语法

[英]syntax for advanced custom fields in wordpress

I am trying to display a field, but it shows the value fine, but OUTSIDE of the tags?!??! 我正在尝试显示一个字段,但是它显示的值很好,但是标签的外面呢?

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

Output = 输出=

"London"
<h2></h2>

Should be = 应该是=

<h2>London</h2>

Because you have a function like this: 因为您具有这样的功能:

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

Change your function to: 将功能更改为:

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

Why? 为什么? Because PHP executes the function before printing output of the echo. 因为PHP在打印回显的输出之前执行了该功能。

Use this: 用这个:

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

Explanation: 说明:

You code has the output it has because of how echo works. 由于echo工作原理,您的代码具有输出。 It first generates the whole string (running the functions), and then it renders the output. 它首先生成整个字符串(运行函数),然后呈现输出。 So, if the function the_field has output, it will generate what you see. 因此,如果函数the_field具有输出,它将生成您所看到的内容。

Basically your code is equivalent to: 基本上,您的代码等效于:

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

Example: 例:

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

And here is the result: 结果如下:

$ php test.php
1PRE - 2 - POST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM