当前浏览:首页 / WordPress函数 / wp_get_theme()

wp_get_theme( string $stylesheet = '', string $theme_root = '' )

获取主题的WP_Theme对象

theme 主题more...


参数

$stylesheet

(string)(可选) 主题的目录名。默认为启用的主题。

默认值: ''

$theme_root

(string)(可选) 要查找的主题的根绝对路径。如果未指定,则使用get_raw_theme_root()计算提供的$stylesheet(或启用的主题)的主题根路径。

默认值: ''


返回

(WP_Theme) 主题对象。如果需要确认主题的存在,请确保检查对象的exists()方法。



源码

查看源码 官方文档


更新日志

版本描述
3.4.0开始引入

使用示例

  • 示例1

    回显已安装主题的名称

    <?php
    $my_theme = wp_get_theme( 'twentytwelve' );
    if ( $my_theme->exists() )
    	echo esc_html( $my_theme );
    ?>
    
    
  • 示例2

    结果:

    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)
  • 示例3

    获取其他数据:文本域&主题URI

    <?php
    $my_theme = wp_get_theme();
    echo esc_html( $my_theme->get( 'TextDomain' ) );
    echo esc_html( $my_theme->get( 'ThemeURI' ) );
    ?>
    
    
  • 示例4

    在多站点安装中的所有站点上切换特定主题

    <?php
    function wpdocs_switch_all_multisite_themes() {
        foreach ( get_sites() as $site ) {
            switch_to_blog( $site->blog_id );
    	$blog_theme = wp_get_theme();
    	if ( 'twentyeleven' === $blog_theme ) {
                switch_theme( 'twentytwenty' );
                restore_current_blog();
    	}
        }
    }
    
    wpdocs_switch_all_multisite_themes();
    ?>
    
  • 示例5

    获取当前主题的目录名,而不考虑子主题。

    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();
    
  • 示例6

    显示当前主题作者URI

    $my_theme = wp_get_theme();
    echo esc_html( $my_theme->get( 'AuthorURI' ) );
    
  • 示例7

    显示当前主题的版本

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

    回显当前启用主题的名称

    <?php
    echo wp_get_theme();
    ?>