WordPress中输出文章标题的函数有不少,方式也很多,比如the_title、$post->title等等,single_post_title函数也可以输出文章页文章标题,下面是它的结构与用法,看看它与the_title函数的区别在哪儿。
函数描述
显示文章页面标题,single_post_title这个函数没有过滤器,但是你可以自己定义。
函数原型
single_post_title函数位于wp-includes/general-template.php文件中,下面是它的源代码。
复制
function single_post_title( $prefix = '', $display = true ) {
$_post = get_queried_object();
if ( !isset($_post->post_title) )
return;
/**
* Filters the page title for a single post.
*
* @since 0.71
*
* @param string $_post_title The single post page title.
* @param object $_post The current queried object as returned by get_queried_object().
*/
$title = apply_filters( 'single_post_title', $_post->post_title, $_post );
if ( $display )
echo $prefix . $title;
else
return $prefix . $title;
}构造很清楚,结合源代码,我们可以更清晰的看到函数的参数含义。
参数说明
复制
$prefix
字符串值,默认为空
在文章标题前输出的内容
$display
布尔值,默认值:true
是否输出标题,如果为false,只返回结果而不输出。
简单使用
输出当前文章:WordPress函数single_post_title文章页输出文章标题
复制
<h2><?php single_post_title( '当前文章: ' ); ></h2>





评论 (2)