_get_block_templates_files

函数
_get_block_templates_files ( $template_type )
Access
Private
参数
  • (string) $template_type 'wp_template' or 'wp_template_part'.
    Required:
返回值
  • (array) Template.
定义位置
相关方法
_get_block_template_file_get_block_templates_pathsget_block_templatesget_block_templateget_block_theme_folders
引入
5.9.0
弃用
-

_get_block_templates_files: 这是一个用于获取特定文章类型的所有块模板文件的数组的函数。它接收一个文章类型的名称作为参数,并返回一个块模板文件的数组。

从主题中检索模板文件。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function _get_block_templates_files( $template_type ) {
if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
return null;
}
$themes = array(
get_stylesheet() => get_stylesheet_directory(),
get_template() => get_template_directory(),
);
$template_files = array();
foreach ( $themes as $theme_slug => $theme_dir ) {
$template_base_paths = get_block_theme_folders( $theme_slug );
$theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] );
foreach ( $theme_template_files as $template_file ) {
$template_base_path = $template_base_paths[ $template_type ];
$template_slug = substr(
$template_file,
// Starting position of slug.
strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ),
// Subtract ending '.html'.
-5
);
$new_template_item = array(
'slug' => $template_slug,
'path' => $template_file,
'theme' => $theme_slug,
'type' => $template_type,
);
if ( 'wp_template_part' === $template_type ) {
$template_files[] = _add_block_template_part_area_info( $new_template_item );
}
if ( 'wp_template' === $template_type ) {
$template_files[] = _add_block_template_info( $new_template_item );
}
}
}
return $template_files;
}
function _get_block_templates_files( $template_type ) { if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) { return null; } $themes = array( get_stylesheet() => get_stylesheet_directory(), get_template() => get_template_directory(), ); $template_files = array(); foreach ( $themes as $theme_slug => $theme_dir ) { $template_base_paths = get_block_theme_folders( $theme_slug ); $theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] ); foreach ( $theme_template_files as $template_file ) { $template_base_path = $template_base_paths[ $template_type ]; $template_slug = substr( $template_file, // Starting position of slug. strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ), // Subtract ending '.html'. -5 ); $new_template_item = array( 'slug' => $template_slug, 'path' => $template_file, 'theme' => $theme_slug, 'type' => $template_type, ); if ( 'wp_template_part' === $template_type ) { $template_files[] = _add_block_template_part_area_info( $new_template_item ); } if ( 'wp_template' === $template_type ) { $template_files[] = _add_block_template_info( $new_template_item ); } } } return $template_files; }
function _get_block_templates_files( $template_type ) {
	if ( 'wp_template' !== $template_type && 'wp_template_part' !== $template_type ) {
		return null;
	}

	$themes         = array(
		get_stylesheet() => get_stylesheet_directory(),
		get_template()   => get_template_directory(),
	);
	$template_files = array();
	foreach ( $themes as $theme_slug => $theme_dir ) {
		$template_base_paths  = get_block_theme_folders( $theme_slug );
		$theme_template_files = _get_block_templates_paths( $theme_dir . '/' . $template_base_paths[ $template_type ] );
		foreach ( $theme_template_files as $template_file ) {
			$template_base_path = $template_base_paths[ $template_type ];
			$template_slug      = substr(
				$template_file,
				// Starting position of slug.
				strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ),
				// Subtract ending '.html'.
				-5
			);
			$new_template_item = array(
				'slug'  => $template_slug,
				'path'  => $template_file,
				'theme' => $theme_slug,
				'type'  => $template_type,
			);

			if ( 'wp_template_part' === $template_type ) {
				$template_files[] = _add_block_template_part_area_info( $new_template_item );
			}

			if ( 'wp_template' === $template_type ) {
				$template_files[] = _add_block_template_info( $new_template_item );
			}
		}
	}

	return $template_files;
}

常见问题

FAQs
查看更多 >