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

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

get_theme_data()函数

经网友聲色犬馬指正:WordPress 3.4以后,get_theme_data()函数被弃用,虽然试过依然有效,不过还是建议使用wp_get_theme()函数,使用方法文后有详细介绍。

文件

HTML

style.css

函数

php

get_theme_data()

主题信息

默认主题信息如下:

css

/*
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

$theme_name='twentytwentytwo';
$theme_data=get_theme_data(get_theme_root().'/'.$theme_name.'/style.css');
echo$theme_data['Title'];
echo$theme_data['Author'];

示例

函数调用

php

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

简码

php

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

[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

$theme = wp_get_theme( $stylesheet, $theme_root ); 

参数

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

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

示例

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

php

echo wp_get_theme();

显示已安装主题的名称

php

$my_theme = wp_get_theme( 'twentytwentytwo' );
if ( $my_theme->exists() )
	echo $my_theme;

显示当前主题的版本

php

$my_theme = wp_get_theme();
    echo $my_theme->get( 'Version' );

当前主题版本简码:

php

function theme_version(){
    $my_theme = wp_get_theme();
    return $my_theme->get( 'Version' );
}
add_shortcode("theme_version", "theme_version");

最后在文章中插入

html

[theme_version]

完整代码

php

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

最终在文章中的效果

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

一款简单的WordPress主题June

第一次做主题,也没想到好的主题名字,既然是6月份所作,就暂时命名June吧。主题是非有亮点大家自行寻找吧!因为是自用,所有的功能和样式都是根据自己喜好折腾。

那年 • 今日
写于2022-10-29 17:39
酸甜苦辣咸,五味调和,共存相生,百味纷呈。

赞助 点赞 2

灰常记忆, 独元殇, 阿和, 聲色犬馬等人对本文发表了10条热情洋溢的评论。
  • 灰常记忆说道:
    😅厉害会折腾 以为你把主题公开了😂
    1. 老王说道:
      回复 灰常记忆: 发现问题太多,有些自己知道的问题还没解决呢,不敢公布 ::wb:wl::
  • 独元殇说道:
    WordPress 就是专业,那么多属性 (⊙x⊙;)
    1. 老王说道:
      回复 独元殇: 太多好多都用不上,更多的是我不懂。。。 ::wb:wl::
  • 阿和说道:
    默默飘过~~~
    1. 老王说道:
      回复 阿和: 不要路过,说不定哪天你那个弄腻了就换过来了。。。😂
      1. 阿和说道:
        回复 老王: 不是腻不腻的问题,知识储备不允许😂😂😂
        1. 老王说道:
          回复 阿和: 你知道的我的知识储备更加不允许,可以在折腾中学习。。。 ::wb:wl::
  • 聲色犬馬说道:
    路人路過看看,順便好奇看了看 get_theme_data() 源碼的實現,這個函數自 WordPress 3.4.0 時已棄用不建議使用,而且短碼中 $theme_name 寫死了不夠優雅,官方建議使用 wp_get_theme() 代替 get_theme_data(),代碼更優雅更方便,因為它會自動檢索目前啟用主題的 style.css 路徑作為默認目標。
    1. 老王说道:
      回复 聲色犬馬: 谢谢批评指正,已改正!
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用 * 标注