_wp_build_title_and_description_for_single_post_type_block_template

函数
_wp_build_title_and_description_for_single_post_type_block_template ( $post_type, $slug, $template )
Access
Private
参数
  • (string) $post_type Post type, e.g. page, post, product.
    Required:
  • (string) $slug Slug of the post, e.g. a-story-about-shoes.
    Required:
  • (WP_Block_Template) $template Template to mutate adding the description and title computed.
    Required:
返回值
  • (bool) Returns true if the referenced post was found and false otherwise.
定义位置
相关方法
_wp_build_title_and_description_for_taxonomy_block_template_resolve_home_block_template_wp_customize_changeset_filter_insert_post_datawp_underscore_playlist_templateswp_underscore_video_template
引入
6.1.0
弃用
-

_wp_build_title_and_description_for_single_post_type_block_template: 这个函数用来为一个单一的文章类型块模板建立标题和描述。它接收一个文章类型并返回一个包含标题和描述的数组。

根据底层引用的文章,建立特定文章模板的标题和描述。

突变基础模板对象。

function _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, WP_Block_Template $template ) {
	$post_type_object = get_post_type_object( $post_type );

	$default_args = array(
		'post_type'              => $post_type,
		'post_status'            => 'publish',
		'posts_per_page'         => 1,
		'update_post_meta_cache' => false,
		'update_post_term_cache' => false,
		'ignore_sticky_posts'    => true,
		'no_found_rows'          => true,
	);

	$args  = array(
		'name' => $slug,
	);
	$args  = wp_parse_args( $args, $default_args );
	$posts_query = new WP_Query( $args );

	if ( empty( $posts_query->posts ) ) {
		$template->title = sprintf(
			/* translators: Custom template title in the Site Editor referencing a post that was not found. 1: Post type singular name, 2: Post type slug. */
			__( 'Not found: %1$s (%2$s)' ),
			$post_type_object->labels->singular_name,
			$slug
		);

		return false;
	}

	$post_title = $posts_query->posts[0]->post_title;

	$template->title = sprintf(
		/* translators: Custom template title in the Site Editor. 1: Post type singular name, 2: Post title. */
		__( '%1$s: %2$s' ),
		$post_type_object->labels->singular_name,
		$post_title
	);

	$template->description = sprintf(
		/* translators: Custom template description in the Site Editor. %s: Post title. */
		__( 'Template for %s' ),
		$post_title
	);

	$args = array(
		'title' => $post_title,
	);
	$args                        = wp_parse_args( $args, $default_args );
	$posts_with_same_title_query = new WP_Query( $args );

	if ( count( $posts_with_same_title_query->posts ) > 1 ) {
		$template->title = sprintf(
			/* translators: Custom template title in the Site Editor. 1: Template title, 2: Post type slug. */
			__( '%1$s (%2$s)' ),
			$template->title,
			$slug
		);
	}

	return true;
}

常见问题

FAQs
查看更多 >