_wp_build_title_and_description_for_taxonomy_block_template

函数
_wp_build_title_and_description_for_taxonomy_block_template ( $taxonomy, $slug, $template )
Access
Private
参数
  • (string) $taxonomy Identifier of the taxonomy, e.g. category.
    Required:
  • (string) $slug Slug of the term, e.g. shoes.
    Required:
  • (WP_Block_Template) $template Template to mutate adding the description and title computed.
    Required:
返回值
  • (bool) True if the term referenced was found and false otherwise.
定义位置
相关方法
_wp_build_title_and_description_for_single_post_type_block_template_wp_filter_taxonomy_basewp_print_file_editor_templateswp_should_load_separate_core_block_assets_resolve_home_block_template
引入
6.1.0
弃用
-

_wp_build_title_and_description_for_taxonomy_block_template: 该函数用于为分类法块模板建立标题和描述。它接收一个分类法,并返回一个包含标题和描述的数组。

根据所引用的基础实体,建立分类法专用模板的标题和描述。

突变基础模板对象。

function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, WP_Block_Template $template ) {
	$taxonomy_object = get_taxonomy( $taxonomy );

	$default_args = array(
		'taxonomy'               => $taxonomy,
		'hide_empty'             => false,
		'update_term_meta_cache' => false,
	);

	$term_query = new WP_Term_Query();

	$args  = array(
		'number' => 1,
		'slug'   => $slug,
	);
	$args        = wp_parse_args( $args, $default_args );
	$terms_query = $term_query->query( $args );

	if ( empty( $terms_query->terms ) ) {
		$template->title = sprintf(
			/* translators: Custom template title in the Site Editor, referencing a taxonomy term that was not found. 1: Taxonomy singular name, 2: Term slug. */
			__( 'Not found: %1$s (%2$s)' ),
			$taxonomy_object->labels->singular_name,
			$slug
		);
		return false;
	}

	$term_title = $terms_query->terms[0]->name;

	$template->title = sprintf(
		/* translators: Custom template title in the Site Editor. 1: Taxonomy singular name, 2: Term title. */
		__( '%1$s: %2$s' ),
		$taxonomy_object->labels->singular_name,
		$term_title
	);

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

	$term_query = new WP_Term_Query();

	$args = array(
		'number' => 2,
		'name'   => $term_title,
	);
	$args                        = wp_parse_args( $args, $default_args );
	$terms_with_same_title_query = $term_query->query( $args );

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

	return true;
}

常见问题

FAQs
查看更多 >