WordPress函数:post_type_archive_title 不同文章类型不同标题风格

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

post_type_archive_title 是针对archive.php和archive-{$post_type}.php模板文件进行优化的,用于显示特定文章类型档列表页的标题。


函数原型:


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


参数说明:


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


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


函数源码:


function post_type_archive_title( $prefix = '', $display = true ) {
	if ( ! is_post_type_archive() ) {
		return;
	}

	$post_type = get_query_var( 'post_type' );
	if ( is_array( $post_type ) ) {
		$post_type = reset( $post_type );
	}

	$post_type_obj = get_post_type_object( $post_type );

	$title = apply_filters( 'post_type_archive_title', $post_type_obj->labels->name, $post_type );

	if ( $display ) {
		echo $prefix . $title;
	} else {
		return $prefix . $title;
	}
}


包含钩子:


apply_filters( 'post_type_archive_title', string $post_type_name, string $post_type )


使用举例:


<h1><?php post_type_archive_title(); ?></h1>


发表评论

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

客服 工单