使用php将Wordpress标题插入到html中


Insert Wordpress title into html with php

我知道这是一个简单的问题,我可能会因此受到抨击,但我无法让它发挥作用。

当前标题打印在h2之前,并且h2为空。

echo '<h2 class="profile-title">' . the_title() . '</h2>';

感谢

使用get_the_title()。Wordpress中的标准规则:the_whatever()立即执行OUTPUT,而get_the_whatever()返回。因此,the_title()立即输出标题,不返回任何内容,然后回声的其余部分开始。

the_title()用于打印标题,而您正使用echo反复打印标题。要在<h2>标记中打印标题,请将其用作:
echo '<h2 class="profile-title">' . get_the_title() . '</h2>';

默认情况下,

the_title()会打印出该值。

通过传递true作为第三个参数,您可以返回该值,以便在echoprint语句中使用它,如以下

echo '<h2 class="profile-title">' . the_title(null,null,true) . '</h2>';

或者您可以使用第一个和第二个参数beforeafter来构建您想要的标题

the_title('<h2 class="profile-title">','</h2>');

或者,如果你仍然想自己打印

echo the_title('<h2 class="profile-title">','</h2>',true);