get_themes()
云策文档标注
概述
get_themes() 是一个已弃用的 WordPress 函数,用于从主题目录检索主题列表及其数据。自 WordPress 3.4.0 起,建议使用 wp_get_themes() 替代。
关键要点
- 函数 get_themes() 已弃用,自版本 3.4.0 起应使用 wp_get_themes()。
- 返回一个包含主题数据的数组,主题数据基于主题目录中的文件。
- 主题被视为损坏的条件:无父主题时缺少 style.css 或 index.php;有父主题时缺少 style.css(index.php 可选)。
- 函数内部处理主题名称冲突,通过添加样式表路径作为键来避免重复。
代码示例
function get_themes() {
_deprecated_function( __FUNCTION__, '3.4.0', 'wp_get_themes()' );
global $wp_themes;
if ( isset( $wp_themes ) )
return $wp_themes;
$themes = wp_get_themes();
$wp_themes = array();
foreach ( $themes as $theme ) {
$name = $theme->get('Name');
if ( isset( $wp_themes[ $name ] ) )
$wp_themes[ $name . '/' . $theme->get_stylesheet() ] = $theme;
else
$wp_themes[ $name ] = $theme;
}
return $wp_themes;
}注意事项
- 此函数已弃用,新代码中应避免使用,改用 wp_get_themes() 以保持兼容性和最佳实践。
- 主题损坏的判断逻辑基于文件存在性,开发者需确保主题文件完整以避免问题。
原文内容
Retrieve list of themes with theme data in theme directory.
Description
The theme is broken, if it doesn’t have a parent theme and is missing either style.css and, or index.php. If the theme has a parent theme then it is broken, if it is missing style.css; index.php is optional.
See also
Source
function get_themes() {
_deprecated_function( __FUNCTION__, '3.4.0', 'wp_get_themes()' );
global $wp_themes;
if ( isset( $wp_themes ) )
return $wp_themes;
$themes = wp_get_themes();
$wp_themes = array();
foreach ( $themes as $theme ) {
$name = $theme->get('Name');
if ( isset( $wp_themes[ $name ] ) )
$wp_themes[ $name . '/' . $theme->get_stylesheet() ] = $theme;
else
$wp_themes[ $name ] = $theme;
}
return $wp_themes;
}
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Deprecated. Use wp_get_themes() |
| 1.5.0 | Introduced. |