当前浏览:首页 / WordPress函数 / the_title()

the_title( string $before = '', string $after = '', bool $echo = true )

显示或检索带有可选标记的当前文章标题

themore...

titlemore...


参数

$before

(string) (可选) 要在标题前添加的标记。

默认值: ''

$after

(string) (可选) 要附加到标题的标记。

默认值: ''

$echo

(bool) (可选) 回显输出还是返回标题,默认值为true。

默认值: true


返回

(void|string) 如果$echo参数为true则无返回,flase则返回当前文章标题


说明

此函数用于显示或返回当前文章的未转义标题。只能在循环内使用,循环外获取文章标题使用get_the_title。如果该文章是受保护的或私有的,则会在标题前加上“受保护的:”或“私有的:”字样。

安全注意事项

the_content()一样,the_title()的输出是未转义的。这被认为是一个特性,而不是一个bug,请参见the FAQ “Why are some users allowed to post unfiltered HTML?”。如果文章标题是<script>alert("test");</script>,那么无论在哪里使用the_title(),JavaScript代码都将运行。因此,不要编写允许不受信任的用户创建文章标题的代码。



源码

查看源码 官方文档


更新日志

版本描述
0.71开始引入

使用示例

  • 示例1

    示例

    <?php the_title( '<h3>', '</h3>' ); ?>
    

    这会将标题打印到屏幕上作为h3。

  • 示例2

    简单用例

    <?php
    the_title(
      sprintf( '<h2 class="card-title h4"><a href="%s" rel="bookmark">', esc_attr( esc_url( get_permalink() ) ) ),
      '</a></h2>'
    );
    ?>