函数文档

get_block_theme_folders()

💡 云策文档标注

概述

get_block_theme_folders() 函数用于获取块主题使用的文件夹名称,确保向后兼容性,支持 block-templates 和 block-template-parts 目录。

关键要点

  • 函数返回一个字符串数组,包含块主题的模板和模板部分文件夹名称。
  • 参数 $theme_stylesheet 可选,默认为 null,用于指定主题样式表以获取对应主题的文件夹。
  • 如果主题不存在,函数返回默认文件夹:'wp_template' 对应 'templates','wp_template_part' 对应 'parts'。
  • 函数内部使用 wp_get_theme() 获取 WP_Theme 对象,并调用 get_block_template_folders() 方法。
  • 该函数自 WordPress 5.9.0 版本引入。

代码示例

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',
        );
    }
    return $theme->get_block_template_folders();
}

📄 原文内容

For backward compatibility reasons, block themes might be using block-templates or block-template-parts, this function ensures we fallback to these folders properly.

Parameters

$theme_stylesheetstringoptional
The stylesheet. Default is to leverage the main theme root.

Default:null

Return

string[] Folder names used by block themes.

  • wp_template string
    Theme-relative directory name for block templates.
  • wp_template_part string
    Theme-relative directory name for block template parts.

Source

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',
		);
	}
	return $theme->get_block_template_folders();
}

Changelog

Version Description
5.9.0 Introduced.