函数文档

_wp_build_title_and_description_for_taxonomy_block_template()

💡 云策文档标注

概述

此函数用于基于引用的分类法实体,为特定分类法的块模板构建标题和描述。它会修改传入的 WP_Block_Template 对象,添加计算出的标题和描述。

关键要点

  • 函数 _wp_build_title_and_description_for_taxonomy_block_template 接受三个参数:$taxonomy(分类法标识符,如 category)、$slug(术语 slug,如 shoes)和 $template(要修改的 WP_Block_Template 对象)。
  • 通过 WP_Term_Query 查询术语,如果未找到术语,则设置模板标题为“Not found: [分类法单数名称] ([术语 slug])”并返回 false。
  • 如果找到术语,则设置模板标题为“[分类法单数名称]: [术语名称]”,描述为“Template for [术语名称]”。
  • 如果存在多个同名术语,则在标题后附加术语 slug 以避免冲突。
  • 函数返回布尔值:找到术语时返回 true,否则返回 false。

代码示例

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 ) ) {
        $template->title = sprintf(
            __( 'Not found: %1$s (%2$s)' ),
            $taxonomy_object->labels->singular_name,
            $slug
        );
        return false;
    }

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

    $template->title = sprintf(
        __( '%1$s: %2$s' ),
        $taxonomy_object->labels->singular_name,
        $term_title
    );

    $template->description = sprintf(
        __( '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 ) > 1 ) {
        $template->title = sprintf(
            __( '%1$s (%2$s)' ),
            $template->title,
            $slug
        );
    }

    return true;
}

注意事项

  • 此函数会直接修改传入的 WP_Block_Template 对象,调用时需注意对象状态变化。
  • 依赖于 get_taxonomy、WP_Term_Query、__() 和 wp_parse_args 等 WordPress 核心函数,确保环境支持。
  • 翻译字符串使用 __() 函数,便于国际化。
  • 自 WordPress 6.1.0 版本引入。

📄 原文内容

Builds the title and description of a taxonomy-specific template based on the underlying entity referenced.

Description

Mutates the underlying template object.

Parameters

$taxonomystringrequired
Identifier of the taxonomy, e.g. category.
$slugstringrequired
Slug of the term, e.g. shoes.
$templateWP_Block_Templaterequired
Template to mutate adding the description and title computed.

Return

bool True if the term referenced was found and false otherwise.

Source

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 ) ) {
		$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[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 ) > 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;
}

Changelog

Version Description
6.1.0 Introduced.