render_block_core_query_title

函数
render_block_core_query_title ( $attributes )
参数
  • (array) $attributes Block attributes.
    Required:
返回值
  • (string) Returns the query title based on the queried object.
定义位置
相关方法
register_block_core_query_titlerender_block_core_site_titlerender_block_core_post_titlerender_block_core_query_paginationrender_block_core_file
引入
-
弃用
-

render_block_core_query_title: 这个函数用来渲染WordPress中的查询块的标题。它负责为该块的标题生成HTML标记,用户可以自定义。

在服务器上渲染`core/query-title’区块。

目前,它只支持Archive title,使用查询的对象信息。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function render_block_core_query_title( $attributes ) {
$type = isset( $attributes['type'] ) ? $attributes['type'] : null;
$is_archive = is_archive();
$is_search = is_search();
if ( ! $type ||
( 'archive' === $type && ! $is_archive ) ||
( 'search' === $type && ! $is_search )
) {
return '';
}
$title = '';
if ( $is_archive ) {
$show_prefix = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true;
if ( ! $show_prefix ) {
$filter_title = function( $title, $original_title ) {
return $original_title;
};
add_filter( 'get_the_archive_title', $filter_title, 10, 2 );
$title = get_the_archive_title();
remove_filter( 'get_the_archive_title', $filter_title, 10, 2 );
} else {
$title = get_the_archive_title();
}
}
if ( $is_search ) {
$title = __( 'Search results' );
if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {
$title = sprintf(
/* translators: %s is the search term. */
__( 'Search results for: "%s"' ),
get_search_query()
);
}
}
$tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
$align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
return sprintf(
'<%1$s %2$s>%3$s</%1$s>',
$tag_name,
$wrapper_attributes,
$title
);
}
function render_block_core_query_title( $attributes ) { $type = isset( $attributes['type'] ) ? $attributes['type'] : null; $is_archive = is_archive(); $is_search = is_search(); if ( ! $type || ( 'archive' === $type && ! $is_archive ) || ( 'search' === $type && ! $is_search ) ) { return ''; } $title = ''; if ( $is_archive ) { $show_prefix = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true; if ( ! $show_prefix ) { $filter_title = function( $title, $original_title ) { return $original_title; }; add_filter( 'get_the_archive_title', $filter_title, 10, 2 ); $title = get_the_archive_title(); remove_filter( 'get_the_archive_title', $filter_title, 10, 2 ); } else { $title = get_the_archive_title(); } } if ( $is_search ) { $title = __( 'Search results' ); if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) { $title = sprintf( /* translators: %s is the search term. */ __( 'Search results for: "%s"' ), get_search_query() ); } } $tag_name = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1'; $align_class_name = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}"; $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) ); return sprintf( '<%1$s %2$s>%3$s</%1$s>', $tag_name, $wrapper_attributes, $title ); }
function render_block_core_query_title( $attributes ) {
	$type       = isset( $attributes['type'] ) ? $attributes['type'] : null;
	$is_archive = is_archive();
	$is_search  = is_search();
	if ( ! $type ||
		( 'archive' === $type && ! $is_archive ) ||
		( 'search' === $type && ! $is_search )
		) {
		return '';
	}
	$title = '';
	if ( $is_archive ) {
		$show_prefix = isset( $attributes['showPrefix'] ) ? $attributes['showPrefix'] : true;
		if ( ! $show_prefix ) {
			$filter_title = function( $title, $original_title ) {
				return $original_title;
			};
			add_filter( 'get_the_archive_title', $filter_title, 10, 2 );
			$title = get_the_archive_title();
			remove_filter( 'get_the_archive_title', $filter_title, 10, 2 );
		} else {
			$title = get_the_archive_title();
		}
	}
	if ( $is_search ) {
		$title = __( 'Search results' );

		if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {
			$title = sprintf(
				/* translators: %s is the search term. */
				__( 'Search results for: "%s"' ),
				get_search_query()
			);
		}
	}

	$tag_name           = isset( $attributes['level'] ) ? 'h' . (int) $attributes['level'] : 'h1';
	$align_class_name   = empty( $attributes['textAlign'] ) ? '' : "has-text-align-{$attributes['textAlign']}";
	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $align_class_name ) );
	return sprintf(
		'<%1$s %2$s>%3$s</%1$s>',
		$tag_name,
		$wrapper_attributes,
		$title
	);
}

常见问题

FAQs
查看更多 >