locate_block_template

函数
locate_block_template ( $template, $type, $templates )
参数
  • (string) $template Path to the template. See locate_template().
    Required:
  • (string) $type Sanitized filename without extension.
    Required:
  • (string[]) $templates A list of template candidates, in descending order of priority.
    Required:
返回值
  • (string) The path to the Full Site Editing template canvas file, or the fallback PHP template.
定义位置
相关方法
locate_templateget_block_templateget_block_templatesresolve_block_templatewp_enable_block_templates
引入
5.8.0
弃用
-

locate_block_template: 这是WordPress中的一个函数,它允许你在你的主题或插件中定位一个块模板文件。你可以使用这个函数以编程方式定位特定块的模板文件,然后在你的代码中包含或修改它。

找到一个比给定的PHP模板文件具有相同或更高特异性的块模板。

在内部,这通过一个全局变量来传达需要被模板画布使用的区块内容。

function locate_block_template( $template, $type, array $templates ) {
	global $_wp_current_template_content;

	if ( ! current_theme_supports( 'block-templates' ) ) {
		return $template;
	}

	if ( $template ) {
		/*
		 * locate_template() has found a PHP template at the path specified by $template.
		 * That means that we have a fallback candidate if we cannot find a block template
		 * with higher specificity.
		 *
		 * Thus, before looking for matching block themes, we shorten our list of candidate
		 * templates accordingly.
		 */

		// Locate the index of $template (without the theme directory path) in $templates.
		$relative_template_path = str_replace(
			array( get_stylesheet_directory() . '/', get_template_directory() . '/' ),
			'',
			$template
		);
		$index                  = array_search( $relative_template_path, $templates, true );

		// If the template hierarchy algorithm has successfully located a PHP template file,
		// we will only consider block templates with higher or equal specificity.
		$templates = array_slice( $templates, 0, $index + 1 );
	}

	$block_template = resolve_block_template( $type, $templates, $template );

	if ( $block_template ) {
		if ( empty( $block_template->content ) && is_user_logged_in() ) {
			$_wp_current_template_content =
			sprintf(
				/* translators: %s: Template title */
				__( 'Empty template: %s' ),
				$block_template->title
			);
		} elseif ( ! empty( $block_template->content ) ) {
			$_wp_current_template_content = $block_template->content;
		}
		if ( isset( $_GET['_wp-find-template'] ) ) {
			wp_send_json_success( $block_template );
		}
	} else {
		if ( $template ) {
			return $template;
		}

		if ( 'index' === $type ) {
			if ( isset( $_GET['_wp-find-template'] ) ) {
				wp_send_json_error( array( 'message' => __( 'No matching template found.' ) ) );
			}
		} else {
			return ''; // So that the template loader keeps looking for templates.
		}
	}

	// Add hooks for template canvas.
	// Add viewport meta tag.
	add_action( 'wp_head', '_block_template_viewport_meta_tag', 0 );

	// Render title tag with content, regardless of whether theme has title-tag support.
	remove_action( 'wp_head', '_wp_render_title_tag', 1 );    // Remove conditional title tag rendering...
	add_action( 'wp_head', '_block_template_render_title_tag', 1 ); // ...and make it unconditional.

	// This file will be included instead of the theme's template file.
	return ABSPATH . WPINC . '/template-canvas.php';
}

常见问题

FAQs
查看更多 >