函数文档

_build_template_result_from_post()

💡 云策文档标注

概述

_build_template_result_from_post() 函数基于 WP_Post 对象构建统一的模板对象,返回 WP_Block_Template 或 WP_Error。该函数涉及块主题的模板文件夹处理,确保向后兼容性。

关键要点

  • 函数参数:$post(WP_Post 对象,必需),用于构建模板。
  • 返回值:WP_Block_Template 或 WP_Error 对象。
  • 相关函数:get_block_theme_folders() 用于获取块主题的模板文件夹路径,支持向后兼容。
  • 版本信息:自 WordPress 5.8.0 引入。

代码示例

if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) {
    define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' );
}

📄 原文内容

Build a unified template object based a post Object.

Parameters

$postWP_Postrequired
Template post.

Return

WP_Block_Template|WP_Error Template.

Source

if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) {
	define( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED', 'uncategorized' );
}

/**
 * For backward compatibility reasons,
 * block themes might be using block-templates or block-template-parts,
 * this function ensures we fallback to these folders properly.
 *
 * @since 5.9.0
 *
 * @param string $theme_stylesheet The stylesheet. Default is to leverage the main theme root.
 *
 * @return string[] {
 *     Folder names used by block themes.
 *
 *     @type string $wp_template      Theme-relative directory name for block templates.
 *     @type string $wp_template_part Theme-relative directory name for block template parts.
 * }
 */
function get_block_theme_folders( $theme_stylesheet = null ) {
	$theme = wp_get_theme( (string) $theme_stylesheet );
	if ( ! $theme->exists() ) {
		// Return the default folders if the theme doesn't exist.
		return array(
			'wp_template'      => 'templates',
			'wp_template_part' => 'parts',
		);

Changelog

Version Description
5.8.0 Introduced.