get_current_theme()
云策文档标注
概述
get_current_theme() 函数用于获取当前主题的名称,但自 WordPress 3.4.0 起已被弃用,建议使用 wp_get_theme() 替代。
关键要点
- 函数返回当前主题的名称字符串。
- 自 WordPress 3.4.0 版本起,此函数已被标记为弃用,推荐使用 wp_get_theme() 函数。
- 函数内部首先尝试通过 get_option('current_theme') 获取主题名,失败则回退到 wp_get_theme()->get('Name')。
代码示例
$theme_name = get_current_theme();
echo esc_html( $theme_name );注意事项
- 由于函数已弃用,在新开发中应避免使用,改用 wp_get_theme() 以获取 WP_Theme 对象。
- 使用 esc_html() 输出主题名以确保安全性。
原文内容
Retrieve current theme name.
Description
See also
Source
function get_current_theme() {
_deprecated_function( __FUNCTION__, '3.4.0', 'wp_get_theme()' );
if ( $theme = get_option( 'current_theme' ) )
return $theme;
return wp_get_theme()->get('Name');
}
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Deprecated. Use wp_get_theme() |
| 1.5.0 | Introduced. |
Skip to note 2 content
Codex
Current theme name
Get the name of the current theme.
$theme_name = get_current_theme(); echo esc_html( $theme_name );