!
也想出现在这里? 联系我们
广告位

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

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

函数构造

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

函数原型:

  1. function wp_get_theme( $stylesheet = null, $theme_root = null ) {
  2. global $wp_theme_directories;
  3. if ( empty( $stylesheet ) ) {
  4. $stylesheet = get_stylesheet();
  5. }
  6. if ( empty( $theme_root ) ) {
  7. $theme_root = get_raw_theme_root( $stylesheet );
  8. if ( false === $theme_root ) {
  9. $theme_root = WP_CONTENT_DIR . '/themes';
  10. } elseif ( ! in_array( $theme_root, (array) $wp_theme_directories ) ) {
  11. $theme_root = WP_CONTENT_DIR . $theme_root;
  12. }
  13. }
  14. return new WP_Theme( $stylesheet, $theme_root );
  15. }

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

参数:

$stylesheet

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

默认值: 空

$theme_root

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

默认值: 空

返回值

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

简单使用

  1. <?php
  2. $my_theme = wp_get_theme( 'twentytwelve' );
  3. if ( $my_theme->exists() )
  4. echo esc_HTML( $my_theme );
  5. ?>

结果:

  1. object(WP_Theme)[916]
  2. public 'update' => boolean false
  3. private 'theme_root' => string 'home/path/wp-content/themes' (length=77)
  4. private 'headers' =>
  5. array (size=11)
  6. 'Name' => string 'mytheme' (length=7)
  7. 'ThemeURI' => string 'http://example.com/' (length=22)
  8. 'Description' => string 'Description' (length=11)
  9. 'Author' => string 'Something Here' (length=14)
  10. 'AuthorURI' => string 'http://example.com/' (length=22)
  11. 'Version' => string '1.0.0' (length=5)
  12. 'Template' => string '' (length=0)
  13. 'Status' => string '' (length=0)
  14. 'Tags' => string 'custom-background, custom-logo, custom-menu, featured-images, threaded-comments, translation-ready' (length=98)
  15. 'TextDomain' => string 'mytheme' (length=7)
  16. 'DomainPath' => string '' (length=0)
  17. private 'headers_sanitized' => null
  18. private 'name_translated' => null
  19. private 'errors' => null
  20. private 'stylesheet' => string 'mytheme' (length=7)
  21. private 'template' => string 'mytheme' (length=7)
  22. private 'parent' => null
  23. private 'theme_root_uri' => null
  24. private 'textdomain_loaded' => null
  25. private 'cache_hash' => string 'ca9dd01f01f2a5cb4616a776eff52690' (length=32)

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress自定义文章类型支持古腾堡(Gutenberg)编辑器

2023-12-15 14:10:02

WordPress教程

代码实现WordPress文章网址URL自动生成超链接

2023-12-15 14:10:34

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索