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

WordPress获取主题信息函数wp_get_theme详解,第1张

概述WordPress主题信息在开发主题时可能会用到,一般是在主题的更新功能时使用。wp_get_theme函数可以直接得到当前启用的主题或者指定的主题信息,包括版本作者

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。内存溢出小编现在分享给大家,也给大家做个参考。

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

函数构造@H_404_6@

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 );

}

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

参数:@H_404_6@

$stylesheet

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

默认值: 空

$theme_root

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

默认值: 空

返回值

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

简单使用@H_404_6@

<?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)

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的WordPress获取主题信息函数wp_get_theme详解全部内容,希望文章能够帮你解决WordPress获取主题信息函数wp_get_theme详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/1006951.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存