为什么 wordpress 函数在我的变量之外返回值
Why is wordpress function returning values outside of my variable
我的 wordpress 主题文件夹中的 functions.php 文件中有一个函数。此函数创建一个 link 到 php 文件,您可以在其中下载前端所选 wordpress post 的 ics 文件。
function ics_maker() {
$ics_link = "<a href='https://intranet.local/wp/icsmaker.php?eventname=".the_title()."&eventort=".the_field('eventort')."&eventstart=".the_field('eventstart')."&eventende=".the_field('eventende')."&eventext=".get_the_content()."'>Download</a>";
return $ics_link;
}
add_shortcode( 'shortcode_icsmaker', 'ics_maker');
如果我用
添加这个简码
[shortcode_icsmaker]
我明白了:
TitleofICSAddressOfICS24.03.2022 13:0024.03.2022 17:00 Download
下载 link 可以正常工作 link 但 href excepct 中的变量为空 eventtext:
https://intranet.local/wp/icsmaker.php?eventname=&eventort=&eventstart=&eventende=&eventext=Lorem%20123
似乎 the_title(), the_field("eventort"),... return 本身在 $ics_link 变量之外,但为什么呢?
来自 WordPress 函数参考 (https://developer.wordpress.org/reference/functions/the_title/):
the_title( string $before = '', string $after = '', bool $echo = true )
Display or retrieve the current post title with optional markup.
默认情况下它呼应标题。您需要明确要求返回标题:the_title('', '', false)
或使用 get_the_title()
方法,就像您对 get_the_content()
.
所做的那样
而不是 ACF 的 the_field()
,您有 get_field()
函数,它 returns 字段值而不是回显它 (https://www.advancedcustomfields.com/resources/the_field/)。
我的 wordpress 主题文件夹中的 functions.php 文件中有一个函数。此函数创建一个 link 到 php 文件,您可以在其中下载前端所选 wordpress post 的 ics 文件。
function ics_maker() {
$ics_link = "<a href='https://intranet.local/wp/icsmaker.php?eventname=".the_title()."&eventort=".the_field('eventort')."&eventstart=".the_field('eventstart')."&eventende=".the_field('eventende')."&eventext=".get_the_content()."'>Download</a>";
return $ics_link;
}
add_shortcode( 'shortcode_icsmaker', 'ics_maker');
如果我用
添加这个简码[shortcode_icsmaker]
我明白了:
TitleofICSAddressOfICS24.03.2022 13:0024.03.2022 17:00 Download
下载 link 可以正常工作 link 但 href excepct 中的变量为空 eventtext:
https://intranet.local/wp/icsmaker.php?eventname=&eventort=&eventstart=&eventende=&eventext=Lorem%20123
似乎 the_title(), the_field("eventort"),... return 本身在 $ics_link 变量之外,但为什么呢?
来自 WordPress 函数参考 (https://developer.wordpress.org/reference/functions/the_title/):
the_title( string $before = '', string $after = '', bool $echo = true )
Display or retrieve the current post title with optional markup.
默认情况下它呼应标题。您需要明确要求返回标题:the_title('', '', false)
或使用 get_the_title()
方法,就像您对 get_the_content()
.
而不是 ACF 的 the_field()
,您有 get_field()
函数,它 returns 字段值而不是回显它 (https://www.advancedcustomfields.com/resources/the_field/)。