the_field() 函数将值放在错误的位置
the_field() function puts value in the wrong position
所以我试图获取我创建的自定义字段的值。基本上,我想通过 wordpress 中的自定义字段使用动态给定的 ID 在我的模板中填充 gravityforms 简码(在循环内)。我正在使用 acf get_field 函数,但我不太明白出了什么问题。在这种情况下,gravityform 的 ID 为“3”。
这是我的方法:
echo do_shortcode('[gravityforms id="'.the_field('gf-for-events').'" title="false" description="false" ajax="true"]');
现在我得到的结果是这样的:
3
Oops! We could not locate your form.
输出的ID确实是对的,但是不知道为什么输出错了位置。为了进一步测试这一点,我尝试了这个:
$test = the_field('gf-for-events');
echo ('[gravityforms id="'.$test.'" title="false" description="false" ajax="true"]');
这是输出:
3[gravityforms id="" title="false" description="false" ajax="true"]
我知道它与 the_field 函数有关,因为当对 ID 进行硬编码时,它会以正确的方式输出。
$test = 3;
echo ('[gravityforms id="'.$test.'" title="false" description="false" ajax="true"]');
[gravityforms id="3" title="false" description="false" ajax="true"]
我想了解这里出了什么问题,有什么我遗漏的吗?我可以使用 get_field() 的另一种方法吗?
the_field()
方法会立即输出值。当您改用 get_field()
时,它将被 return 编辑为 return 值,因此您可以将其分配给变量。
然后您可以在代码中的任何位置使用该值:
$value = get_field('gf-for-events');
if ( $value ) {
echo '[gravityforms id="' . $value . '" title="false" description="false" ajax="true"]';
}
这应该输出(如果 return 值确实是 3
):
[gravityforms id="3" title="false" description="false" ajax="true"]
这个在the_field()
and get_field()
的文档中也分别有描述:
the_field(): Displays the value of a specific field.
get_field(): Returns the value of a specific field.
所以我试图获取我创建的自定义字段的值。基本上,我想通过 wordpress 中的自定义字段使用动态给定的 ID 在我的模板中填充 gravityforms 简码(在循环内)。我正在使用 acf get_field 函数,但我不太明白出了什么问题。在这种情况下,gravityform 的 ID 为“3”。
这是我的方法:
echo do_shortcode('[gravityforms id="'.the_field('gf-for-events').'" title="false" description="false" ajax="true"]');
现在我得到的结果是这样的:
3
Oops! We could not locate your form.
输出的ID确实是对的,但是不知道为什么输出错了位置。为了进一步测试这一点,我尝试了这个:
$test = the_field('gf-for-events');
echo ('[gravityforms id="'.$test.'" title="false" description="false" ajax="true"]');
这是输出:
3[gravityforms id="" title="false" description="false" ajax="true"]
我知道它与 the_field 函数有关,因为当对 ID 进行硬编码时,它会以正确的方式输出。
$test = 3;
echo ('[gravityforms id="'.$test.'" title="false" description="false" ajax="true"]');
[gravityforms id="3" title="false" description="false" ajax="true"]
我想了解这里出了什么问题,有什么我遗漏的吗?我可以使用 get_field() 的另一种方法吗?
the_field()
方法会立即输出值。当您改用 get_field()
时,它将被 return 编辑为 return 值,因此您可以将其分配给变量。
然后您可以在代码中的任何位置使用该值:
$value = get_field('gf-for-events');
if ( $value ) {
echo '[gravityforms id="' . $value . '" title="false" description="false" ajax="true"]';
}
这应该输出(如果 return 值确实是 3
):
[gravityforms id="3" title="false" description="false" ajax="true"]
这个在the_field()
and get_field()
的文档中也分别有描述:
the_field(): Displays the value of a specific field.
get_field(): Returns the value of a specific field.