加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

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

发布时间:2020-12-14 14:22:57 所属栏目:wordpress 来源:网络整理
导读:由站长 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。 wordpress 主题信息在开发主题时可能会用到,一般是在主题的更新功能时使用。wp_get_theme 函数可以直接得到当前启用的主题或者指定的主题信息,包括版

以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考

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)

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读