函数文档

wp_get_theme()

💡 云策文档标注

概述

wp_get_theme() 函数用于获取指定或当前活动主题的 WP_Theme 对象,便于开发者访问主题的元数据和属性。该函数接受可选参数来指定主题目录和主题根路径,并返回一个 WP_Theme 实例,开发者应使用 exists() 方法验证主题是否存在。

关键要点

  • 函数返回 WP_Theme 对象,可用于获取主题名称、版本、作者等元数据。
  • 参数 $stylesheet 可选,默认为当前活动主题的目录名;$theme_root 可选,默认为通过 get_raw_theme_root() 计算的路径。
  • 使用 exists() 方法检查主题是否存在,避免操作无效主题对象。
  • 在 WordPress 6.7 及以上版本,建议在“init”钩子之后调用此函数,以避免触发 _load_textdomain_just_in_time 错误。

代码示例

// 获取当前活动主题对象并检查存在性
$my_theme = wp_get_theme();
if ( $my_theme->exists() ) {
    echo esc_html( $my_theme->get( 'Name' ) );
}

// 获取指定主题(如 twentytwentyfour)的版本
$theme_info = wp_get_theme( 'twentytwentyfour' );
echo $theme_info->get( 'Version' );

// 获取当前主题的目录名,考虑子主题情况
function wpdocs_get_current_theme_directory() {
    $current_theme = wp_get_theme();
    if ( $current_theme->exists() && $current_theme->parent() ) {
        $parent_theme = $current_theme->parent();
        if ( $parent_theme->exists() ) {
            return $parent_theme->get_stylesheet();
        }
    } elseif ( $current_theme->exists() ) {
        return $current_theme->get_stylesheet();
    }
    return '';
}

注意事项

  • 在 WordPress 6.7 及以上版本,避免在“init”钩子之前调用 wp_get_theme(),以防止触发 PHP 通知错误。
  • 返回的 WP_Theme 对象可能包含私有属性,如 theme_root 和 headers,应使用公共方法(如 get())访问数据。
  • 在多站点环境中,可结合 switch_theme() 函数批量切换主题,但需谨慎操作。

📄 原文内容

Gets a WP_Theme object for a theme.

Parameters

$stylesheetstringoptional
Directory name for the theme. Defaults to active theme.
$theme_rootstringoptional
Absolute path of the theme root to look in.
If not specified, get_raw_theme_root() is used to calculate the theme root for the $stylesheet provided (or active theme).

Return

WP_Theme Theme object. Be sure to check the object’s exists() method if you need to confirm the theme’s existence.

Source

function wp_get_theme( $stylesheet = '', $theme_root = '' ) {
	global $wp_theme_directories;

	if ( empty( $stylesheet ) ) {
		$stylesheet = get_stylesheet();
	}

	if ( empty( $theme_root ) ) {
		$theme_root = get_raw_theme_root( $stylesheet );
		if ( false === $theme_root ) {
			$theme_root = WP_CONTENT_DIR . '/themes';
		} elseif ( ! in_array( $theme_root, (array) $wp_theme_directories, true ) ) {
			$theme_root = WP_CONTENT_DIR . $theme_root;
		}
	}

	return new WP_Theme( $stylesheet, $theme_root );
}

Changelog

Version Description
3.4.0 Introduced.

User Contributed Notes

  1. Skip to note 12 content

    Result:

    object(WP_Theme)[916]
      public 'update' => boolean false
      private 'theme_root' => string 'home/path/wp-content/themes' (length=77)
      private 'headers' => 
        array (size=11)
          'Name' => string 'mytheme' (length=7)
          'ThemeURI' => string 'http://example.com/' (length=22)
          'Description' => string 'Description' (length=11)
          'Author' => string 'Something Here' (length=14)
          'AuthorURI' => string 'http://example.com/' (length=22)
          'Version' => string '1.0.0' (length=5)
          'Template' => string '' (length=0)
          'Status' => string '' (length=0)
          'Tags' => string 'custom-background, custom-logo, custom-menu, featured-images, threaded-comments, translation-ready' (length=98)
          'TextDomain' => string 'mytheme' (length=7)
          'DomainPath' => string '' (length=0)
      private 'headers_sanitized' => null
      private 'name_translated' => null
      private 'errors' => null
      private 'stylesheet' => string 'mytheme' (length=7)
      private 'template' => string 'mytheme' (length=7)
      private 'parent' => null
      private 'theme_root_uri' => null
      private 'textdomain_loaded' => null
      private 'cache_hash' => string 'ca9dd01f01f2a5cb4616a776eff52690' (length=32)

  2. Skip to note 15 content

    Get the directory name of the current theme regardless of the child theme.

    function wpdocs_get_current_theme_directory(){
        $current_theme_dir  = '';
        $current_theme      = wp_get_theme();
        if( $current_theme->exists() && $current_theme->parent() ){
            $parent_theme = $current_theme->parent();
    
            if( $parent_theme->exists() ){
                $current_theme_dir = $parent_theme->get_stylesheet();
            }
        } elseif( $current_theme->exists() ) {
            $current_theme_dir = $current_theme->get_stylesheet();
        }
    
        return $current_theme_dir;
    }
    
    echo wpdocs_get_current_theme_directory();

  3. Skip to note 16 content

    Code to Display current or any installed theme version:

    Display current theme version:

    $theme_info = wp_get_theme();
    echo $theme_info->get( 'Version' );

    Display any of installed theme version using directory name:

    $theme_info = wp_get_theme( 'twentytwentyfour' );
    echo $theme_info->get( 'Version' );

  4. Skip to note 19 content

    Display the current theme’s version

    $my_theme = wp_get_theme();
    printf( "%1$s is version %2$s",
    	$my_theme->get( 'Name' ),
    	$my_theme->get( 'Version' )
    );