WordPress获取主题信息函数wp_get_theme详解

wordpress 主题信息在开发主题时可能会用到,一般是在主题的更新功能时使用。wp_get_theme 函数可以直接得到当前启用的主题或者指定的主题信息,包括版本作者路径等。就主题更新功能而言,开发者只需要判断返回的主题对象中版本号是否小于最新版本号即可确定是否更新。比如国内著名主题 DUX 就使用了这个方法来实现主题的更新功能,下面看看官方对这个函数的介绍及使用。

函数构造

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

函数原型:

function wp_get_theme( $stylesheet = null, $theme_root = null ) {
    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 ) ) {
            $theme_root = WP_CONTENT_DIR . $theme_root;
        }
    }
 
    return new WP_Theme( $stylesheet, $theme_root );
}

描述:获得当前主题或指定主题的信息。

参数:

$stylesheet

(string) (可选) 指定的主题名,默认当前主题。

默认值: 空

$theme_root

(string) (可选) 主题的绝对路径,如果为空, 则使用 get_raw_theme_root()函数得到的路径 (默认当前主题).

默认值: 空

返回值

(WP_Theme) 主题对象。 如果不知道主题是否存在,请使用主题对象的 exists()方法判断。

简单使用

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

结果:

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)

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » WordPress获取主题信息函数wp_get_theme详解