为什么所有这些字符串都不会打印


Why wont all of this string print?

我只是想生成一个路径,如下所示:

$PhotoName = the_title(); 
$DestinationFile = 'temp/watermarked/';
$DestinationFile .= $PhotoName;
$DestinationFile .= '.jpg';     

the_title();是一个Wordpress函数,可以获取帖子的标题。 如果我只是echo $PhotoName我看到帖子的名称可疑。 但是,如果我echo $DestinationFile它永远不会打印字符串的那部分,所以我会看到类似 temp/watermarked/.jpg 的东西,它永远不会打印$PhotoName作为它的一部分。

您需要

false作为第三个参数传递,以使它返回页面标题给您。

$PhotoName = the_title('', '', false);

有关更多详细信息,请参阅 WordPress 参考。

the_title()是必须在循环中运行的模板标记。 标题应用筛选器并将标题打印到屏幕上。

使用 get_the_title() 返回 php 中的值。

将 foreach 循环与 get_posts() 一起使用时,请使用 $post->post_title 来获取值。 如果要应用过滤器:apply_filters( 'the_title', $post->post_title );