single_post_title
single_post_title 函数,在 WordPress 中也是一个用于显示文章标题的函数,该函数主要用在文章单页中,并且没有进行过太多的封装。算是对 wp_query 这个类的活用了。
最近和标题干上了,所以把基本上所有 WordPress 中 关于文章标题的函数都想搜刮出来,因为实现方式众多,所以最终只打算将几个经过封装过的函数拿出来分析备忘一下,分别是get_the_title、the_title和 wp_title 以及本文 single_post_title 函数。
函数意义详解
single_post_title 函数显示或返回当前文章的标题。
single_post_title 函数用法、参数详解
参数跟 get_the_title、the_title 中讲的差不多。
single_post_title( $prefix, $display );
-
$prefix分隔符 -
$display是否显示
示例
示例也请参考之前讲过的标题函数,如: wp_title
总结
wp_query 这个类是个好东西,推荐有点能力的童鞋还是好好研究下,这里标题的实现主要还是依靠该类下的一个请求对象的返回函数get_queried_object来实现对象的获取,比较值得研究。
single_post_title 函数声明
该函数声明位于 wp-includ/general-template.php 635 – 646行左右的位置
/**
* Display or retrieve page title for post.
*
* This is optimized for single.php template file for displaying the post title.
*
* It does not support placing the separator after the title, but by leaving the
* prefix parameter empty, you can set the title separator manually. The prefix
* does not automatically place a space between the prefix, so if there should
* be a space, the parameter value will need to have it at the end.
*
* @since 0.71
*
* @param string $prefix Optional. What to display before the title.
* @param bool $display Optional, default is true. Whether to display or retrieve title.
* @return string|null Title when retrieving, null when displaying or failure.
*/
function single_post_title($prefix = '', $display = true) {
$_post = get_queried_object();
if ( !isset($_post->post_title) )
return;
$title = apply_filters('single_post_title', $_post->post_title, $_post);
if ( $display )
echo $prefix . $title;
else
return $title;
}