post_type_archive_title

函数
post_type_archive_title ( $prefix = '', $display = true )
参数
  • (string) $prefix Optional. What to display before the title.
    Required:
    Default: (empty)
  • (bool) $display Optional. Whether to display or retrieve title. Default true.
    Required:
    Default: true
返回值
  • (string|void) Title when retrieving, null when displaying or failure.
定义位置
相关方法
get_the_archive_titlethe_archive_titleis_post_type_archiveget_post_type_archive_linkget_post_type_archive_template
引入
3.1.0
弃用
-

post_type_archive_title是一个WordPress函数,用于生成一个文章类型的存档页的标题。它接受一个参数:$post_type(文章类型对象或名称),并以字符串形式返回存档标题。

显示或检索文章类型存档的标题。

这是对 archive.php 和 archive-{$post_type}.php 模板文件的优化,用于显示文章类型的标题。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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 );
/**
* Filters the post type archive title.
*
* @since 3.1.0
*
* @param string $post_type_name Post type 'name' label.
* @param string $post_type 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;
}
}
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 ); /** * Filters the post type archive title. * * @since 3.1.0 * * @param string $post_type_name Post type 'name' label. * @param string $post_type 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; } }
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 );

	/**
	 * Filters the post type archive title.
	 *
	 * @since 3.1.0
	 *
	 * @param string $post_type_name Post type 'name' label.
	 * @param string $post_type      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;
	}
}

常见问题

FAQs
查看更多 >