作为一个 WordPress 主题开发者,我们都知道在 WordPress 主题中 style.css 会设定主题名称、URL、描述、作者、版本等等信息,而为了开发和使用的便捷性很多时候我们需要在主题其它地方调用这些信息,所以这时候就可以直接使用 WordPress 提供的 wp_get_theme()函数来获取当前主题信息。

WordPress wp_get_theme

1
<?php $theme = wp_get_theme( $stylesheet, $theme_root ); ?>
  • $stylesheet,主题的目录名称(字符串)(可选),默认为当前主题。
  • $theme_root,要查看的主题根的绝对路径(字符串)(可选)。如果未指定,将使用 get_raw_theme_root()返回的值。
1
2
3
4
5
6
7
<?php
$theme = wp_get_theme();
echo $theme->get( 'Name' );//主题名
echo $theme->get( ThemeURI' );//主题 URL
echo $theme->get( Description' );//主题描述
echo $theme->get( Author' );//作者名
?>

wp_get_theme()函数详细参数:

  • Name:主题名
  • ThemeURI:主题 URL
  • Description:主题描述
  • Author:作者名
  • AuthorURI:作者 URL
  • Version:主题版本号
  • Template:父主题的文件夹名称,子主题中使用
  • Status:主题状态,如果发布的话
  • Tags:标签
  • TextDomain:主题中用于翻译目的的文本域
  • DomainPath:主题翻译文件路径

当然啦,wp_get_theme 函数还可以指定主题名来获取指定主题的其它信息,如下:

1
2
3
4
5
<?php
$theme = wp_get_theme( 'Fanly' );
if ( $theme->exists() )
	echo $theme;
?>

嗯,关于 wp_get_theme()函数获取 WordPress 主题子凡信息就给大家分享到这里了吧!

泪雪博客原创文章:https://zhangzifan.com/wordpress-wp_get_theme.html

发表评论
登录后参与评论
专注 WordPress 网站优化解决方案! 加入我们