WordPress的主题信息存在于style.css中,如果主题信息在整个主题中需要扮演一定的较色,可以通过WordPress的get_theme_data()函数或者wp_get_theme()获取,wp_get_theme()是WordPress 3.4版产生的替代get_theme_data()的函数。

获取主题信息

在WordPress 3.4之前,使用get_theme_data()函数获取主题信息,需要制定主题的名字。

在WordPress 3.4以后,该函数被弃用,取而代之的是wp_get_theme()

先来看用get_theme_data()获取当前主题信息。

以WordPress的默认主题twentyeleven为例,在functions.php中放上如下所示的代码,并回显结果

$theme_name = strtolower( str_replace ( ' ','',get_current_theme() ) ); 
$theme_data = get_theme_data( get_theme_root() . '/' . $theme_name . '/style.css' );
echo '<pre>'; print_r($theme_data); echo '</pre>';

结果为

Array
(
    [Name] => Twenty Eleven
    [URI] => http://wordpress.org/extend/themes/twentyeleven
    [Description] => The 2011 theme for WordPress is sophisticated, lightweight, and adaptable. Make it yours with a custom menu, header image, and background — then go further with available theme options for light or dark color scheme, custom link colors, and three layout choices. Twenty Eleven comes equipped with a Showcase page template that transforms your front page into a showcase to show off your best content, widget support galore (sidebar, three footer areas, and a Showcase page widget area), and a custom “Ephemera” widget to display your Aside, Link, Quote, or Status posts. Included are styles for print and for the admin editor, support for featured images (as custom header images on posts and pages and as large images on featured “sticky” posts), and special styles for six different post formats.
    [Author] => the WordPress team
    [AuthorURI] => http://wordpress.org/
    [Version] => 1.3
    [Template] => 
    [Status] => publish
    [Tags] => Array
        (
            [0] => dark
            [1] => light
            [2] => white
            [3] => black
            [4] => gray
            [5] => one-column
            [6] => two-columns
            [7] => left-sidebar
            [8] => right-sidebar
            [9] => fixed-width
            [10] => flexible-width
            [11] => custom-background
            [12] => custom-colors
            [13] => custom-header
            [14] => custom-menu
            [15] => editor-style
            [16] => featured-image-header
            [17] => featured-images
            [18] => full-width-template
            [19] => microformats
            [20] => post-formats
            [21] => rtl-language-support
            [22] => sticky-post
            [23] => theme-options
            [24] => translation-ready
        )

    [Title] => Twenty Eleven
    [AuthorName] => the WordPress team
)

返回值是一个数组,使用数组的方式就可以访问其中的元素,例如要读取主题的URL,可以

var_dump ( $theme_data[ 'URI' ] );

用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->Name . " is version " . $my_theme->Version;
?>

2条留言

评论功能已关闭