前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress 主题作者和版本等信息的调用

WordPress 主题作者和版本等信息的调用

作者头像
楚客追梦
发布2022-11-11 20:13:11
5320
发布2022-11-11 20:13:11
举报
文章被收录于专栏:网页杂谈网页杂谈

在写关于主题的文章中涉及到主题最新版本和最新更新日期,不可能每次更新都去修改文章吧,于是想到从数据库中调用再通过简码(短代码)引用,刚开始从数据库获取信息,奇怪的是引用在文章中成功了,但文章后面的评论及评论框都没有了,反复折腾一番也没能解决。最后想到了style.css文件,因为后台关于主题的相关信息就是从这个文件中调用的。

get_theme_data()函数

WordPress 3.4以后,get_theme_data()函数被弃用,虽然试过依然有效,不过还是建议使用wp_get_theme()函数,使用方法文后有详细介绍。

文件

HTML

代码语言:javascript
复制
style.css

函数

php

代码语言:javascript
复制
get_theme_data()

主题信息

默认主题信息如下:

css

代码语言:javascript
复制
/*
Theme Name: Twenty Twenty-Two
Theme URI: https://wordpress.org/themes/twentytwentytwo/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Built on a solidly designed foundation...
Requires at least: 5.9
Tested up to: 6.0
Requires PHP: 5.6
Version: 1.2
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentytwentytwo
Tags: one-column, custom-colors, custom-menu, custom-logo, editor-style...
*/

规则

默认WordPress主题的样式表: 第一行:主题的名字; 第二行:主题的地址; 第三行:主题作者; 第四行:主题作者主页地址。 第五行:主题的描述; 第六行:主题适用于WP版本; 第七行:主题测试于WP版本; 第八行:主题版本

函数分析

该函数将主题文件内的style.css文件中的主题相关信息(也就是每个wordpress的主题样式页头必须遵守的主题描述格式)通过数组返回,需要说明的是该函数没有默认参数,参数必须指定为你的主题文件名。 该函数能够返回的主题信息:

  • Description – wordpress格式的主题描述内容
  • AuthorURI – 主题作者的URI
  • Template – 主题的主模板名称(在wordpress中属于可选填的内容)
  • Version – 主题版本
  • Status – 主题状态(默认值:发布)
  • Tags – 主题标签
  • Author – 主题作者

注意:这些返回值的参数名必须首字母大写,否则将没有正确值返回。

信息获取

需要获取其他信息仅仅需要替换方括号内的内容即可。

php

代码语言:javascript
复制
$theme_name='twentytwentytwo';
$theme_data=get_theme_data(get_theme_root().'/'.$theme_name.'/style.css');
echo$theme_data['Title'];
echo$theme_data['Author'];

示例

函数调用

php

代码语言:javascript
复制
//获取并显示主题版本号
functiontheme_version( ){
     $theme_name='twentytwentytwo';        //你所使用的主题名
     $theme_data=get_theme_data(get_theme_root().'/'.$theme_name.'/style.css');
     echo'?v='.$theme_data['Version'];
}

简码

php

代码语言:javascript
复制
function theme_version( ){
      $theme_name='twentytwentytwo'; 
      $theme_data=get_theme_data(get_theme_root().'/'.$theme_name.'/style.css');
      return $theme_data['Version'];
}
add_shortcode("theme_version", "theme_version");

最后在文章中插入

html

代码语言:javascript
复制
[theme_version]

聲色犬馬2022-10-30 03:47:20

路人路過看看,順便好奇看了看 get_theme_data() 源碼的實現,這個函數自 WordPress 3.4.0 時已棄用不建議使用,而且短碼中 $theme_name 寫死了不夠優雅,官方建議使用 wp_get_theme() 代替 get_theme_data(),代碼更優雅更方便,因為它會自動檢索目前啟用主題的 style.css 路徑作為默認目標。

感谢网友聲色犬馬的指正,也可以使用wp_get_theme()函数实现。

wp_get_theme()函数

用法

php

代码语言:javascript
复制
$theme = wp_get_theme( $stylesheet, $theme_root ); 

参数

$stylesheet (string) (可选) 主题的目录名。默认为当前主题。 默认值: Null

$theme_root (string) (可选) 要查看的主题根的绝对路径。如果未指定,将使用get_raw_theme_root()返回的值。 默认值: Null

示例

显示当前激活的主题的名称

php

代码语言:javascript
复制
echo wp_get_theme();

显示已安装主题的名称

php

代码语言:javascript
复制
$my_theme = wp_get_theme( 'twentytwentytwo' );
if ( $my_theme->exists() )
	echo $my_theme;

显示当前主题的版本

php

代码语言:javascript
复制
$my_theme = wp_get_theme();
    echo $my_theme->get( 'Version' );

当前主题版本简码:

php

代码语言:javascript
复制
function theme_version(){
    $my_theme = wp_get_theme();
    return $my_theme->get( 'Version' );
}
add_shortcode("theme_version", "theme_version");

最后在文章中插入

html

代码语言:javascript
复制
[theme_version]

完整代码

php

代码语言:javascript
复制
function wp_get_theme( $stylesheet = '', $theme_root = '' ) {
    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 );
}

最终在文章中的效果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • get_theme_data()函数
    • 文件
      • 函数
        • 主题信息
          • 规则
            • 函数分析
              • 信息获取
                • 示例
                • wp_get_theme()函数
                  • 用法
                    • 参数
                      • 示例
                        • 完整代码
                        相关产品与服务
                        网站建设
                        网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档