wordpress获取主题信息wp_get_theme()函数详解

6个月前 (12-06) 浅唱丶
4分钟
158
0

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 ( ! 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()方法判断。

返回值

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

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

使用示例

<?php
    $theme = wp_get_theme()();
    echo $theme->get( 'Name' ); //主题名
    echo $theme->get( ThemeURI' ); //主题 URL
    echo $theme->get( Description' ); //主题描述
    echo $theme->get( Author' ); //作者名
    // 其他信息可以参考文中【返回值】进行调用
?>
本文由:浅唱丶 发布于 小菜的IT网,转载需注明出处:https://www.xiaocaiit.cn/551.html
浅唱丶
作者

相关推荐

6个月前 (12-10)

worpdree开启调试模式WP_DEBUG帮助开发

wordpress开启调试模式可以帮助主题开发者更好的浏览错误信息,从而提升开发的效率。所以wordpress提供了调试模式开启的选项,我们可以在开发环境下使用调试模式。 WordPress开启 WP_DEBUG 调试模式 编辑网站根目录下...
6个月前 (12-06)

WordPress6.0新增文章图片过滤器

以往开发wordpress主题或者插件时,遇到需要更改文章内部的图片相关的功能时候,通常是使用the_content 过滤钩子通过正则表达式进行获取图片进行修改实现的。 但是正则表达式比较复杂,掌握不好非常容易出现错误,现在官方给我们提供了...

评论

已有0人参与了评论

扫一扫关注我们