wp_get_theme()函数

WordPress 开发评论17阅读模式

WordPress主题信息一般包含在Style.css文件中,包括当前主题名称、版本、开发者信息等;如果在开发中某个功能需要用到主题的信息可以通过wp_get_theme()函数进行获取。

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{
            $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()方法判断

 

返回值

返回的为WP_Theme对象类型,通过->进行使用

  • [Name]:主题名称
  • [ThemeURI]:主题链接
  • [Description]:主题介绍
  • [Author]:作者名称
  • [AuthorURI]:作者主页链接
  • [Version]:主题版本
  • [Template]:父主题的文件夹名称,子主题中使用
  • [Status]:主题状态
  • [Tags]:标签
  • [TextDomain]:主题中用于翻译目的的文本域
  • [DomainPath]:主题翻译文件路径
  • [RequiresWP]:要求wordpress的版本
  • [RequiresPHP]:要求php的版本
  • [UpdateURI]:更新链接

使用示例

$theme = wp_get_theme();
echo $theme->get( 'Name' ); //主题名
echo $theme->get( 'ThemeURI' ); //主题 URL
echo $theme->get( 'Description' ); //主题描述
echo $theme->get( 'Author' ); //作者名
// 其他信息可以参考文中【返回值】进行调用

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证