WordPress函数:single_post_title 文章页标题专用

江河/ 2023年04月25日/ WordPress/ 浏览 933

文章是 WordPress 的核心。single_post_title 是针对single.php模板文件进行优化的,用于显示文章标题。


它不支持在标题后放置分隔符,但通过将前缀参数留空,可以手动设置标题分隔符。前缀不会自动在前缀之间放置空格,因此如果应该有空格,则参数值需要在末尾有空格。


函数原型:


single_post_title( string $prefix = '', bool $display = true ): string|void


参数说明:


$prefix,标题前缀,在文章标题前显示。


$display,是否直接显示,还是返回一个字符串。


函数源码:


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 $prefix . $title;
	}
}


包含钩子:


apply_filters( 'single_post_title', string $_post_title, WP_Post $_post )


使用举例:


<h2><?php single_post_title( 'Current post: ' ); ?></h2>


发表评论

暂无评论,抢个沙发...

客服 工单