WordPress 获取主题样式表 style.css 注释里的信息

我们在 Style.css 中都会填入自己的主题信息,例如名称或版本号等,以便 WordPress 展现给用户。以下是WP官方 2018 主题的注释:

/*
Theme Name: Twenty Seventeen
Theme URI: https://wordpress.org/themes/twentyseventeen/
Author: the WordPress team
Author URI: https://wordpress.org/
Description: Twenty Seventeen brings your site to life with header video and immersive featured images. With a focus on business sites, it features multiple sections on the front page as well as widgets, navigation and social menus, a logo, and more. Personalize its asymmetrical grid with a custom color scheme and showcase your multimedia content with post formats. Our default theme for 2017 works great in many languages, for any abilities, and on any device.
Version: 1.7
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentyseventeen
Tags: one-column, two-columns, right-sidebar, flexible-header, accessibility-ready, custom-colors, custom-header, custom-menu, custom-logo, editor-style, featured-images, footer-widgets, post-formats, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready

This theme, like WordPress, is licensed under the GPL.
Use it to make something cool, have fun, and share what you've learned with others.
*/

从中可以看到主题名称、主题链接、作者、作者链接、版本等等信息。在 WordPress 3.4 以上的版本中,我们可以直接使用 wp_get_theme() 这个函数来获取这些信息。以下是一些简单的例子:

获取当前主题名字:

<?php
echo wp_get_theme();
?>

获取其它主题名字:

<?php
$my_theme = wp_get_theme( 'twentyten' );
if ( $my_theme->exists() )
	echo $my_theme;
?>

获取当前主题名称和版本号:

<?php
$my_theme = wp_get_theme();
echo $my_theme->get( 'Name' ) . " is version " . $my_theme->get( 'Version' );
?>

显示当前主题作者的链接:

<?php
$my_theme = wp_get_theme();
echo $my_theme->get( 'AuthorURI' );
?>

更多介绍,请阅读WP官方的 wp_get_theme() 文档

© 版权声明

给TA赞助
共{{data.count}}人
人已赞助
Wordpress

WordPress主题随机显示缩略图

2018-9-8 10:58:41

Wordpress

WordPress博客页脚添加百度收录量

2018-9-26 18:42:34

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
文章目录

『权戈网络』欢迎您!😊