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

管理员
管理员
管理员
238
文章
18
评论
2018年9月13日13:27:15 评论 687 1546字

我们在 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() 文档

weinxin
扫码关注微信公众
幂彀社区,一个关注互联网、平面设计等领域个人博客;致力于打造一个优秀的资源共享学习平台。
管理员
  • 本文由 发表于 2018年9月13日13:27:15
  • 转载请务必保留本文链接:https://www.teelm.com/2018/09/13/12158.html