wp_get_theme()
Получает объект WP_Theme, который содержит информацию о текущей теме.
Аналогичная функция для получения данных плагинов: get_plugins().
Работает на основе: WP_Theme()
1 раз — 0.00056 сек (медленно) | 50000 раз — 2.02 сек (быстро) | PHP 7.0.5, WP 4.5.2
Хуков нет.
Возвращает
WP_Theme. Экземпляр объекта WP_Theme.
Использование
$theme = wp_get_theme( $stylesheet, $theme_root );
- $stylesheet(строка)
- Название каталога в котором находится тема, обычно оно совпадает с названием самой темы. По умолчанию название текущей темы.
По умолчанию: null - $theme_root(строка)
- Полный путь до каталога в котором расположена тема. По умолчанию, путь получается функцией get_raw_theme_root().
По умолчанию: null
Дополнительная информация
Данные можно получать как свойства объекта:
$theme = wp_get_theme(); print_r( [ 'name' => $theme->name, 'title' => $theme->title, 'version' => $theme->version, 'parent_theme' => $theme->parent_theme, 'template_dir' => $theme->template_dir, 'stylesheet_dir' => $theme->stylesheet_dir, 'template' => $theme->template, 'stylesheet' => $theme->stylesheet, 'screenshot' => $theme->screenshot, 'description' => $theme->description, 'author' => $theme->author, 'tags' => $theme->tags, 'theme_root' => $theme->theme_root, 'theme_root_uri' => $theme->theme_root_uri, ] ); /* Получим: Array ( [name] => Twenty Twenty-Three [title] => Twenty Twenty-Three [version] => 1.6 [parent_theme] => [template_dir] => /home/wordpress/wp-content/themes/twentytwentythree [stylesheet_dir] => /home/wordpress/wp-content/themes/twentytwentythree [template] => twentytwentythree [stylesheet] => twentytwentythree [screenshot] => screenshot.png [description] => Twenty Twenty-Three is designed ... [author] => <a href="https://wordpress.org">the WordPress team</a> [tags] => Array ( [0] => one-column [1] => custom-colors [2] => custom-menu ) [theme_root] => /home/wordpress/wp-content/themes [theme_root_uri] => https://exmaple.com.com/wp-content/themes ) */
Также, данные можно получать как элементы массива:
$theme = wp_get_theme(); print_r( [ 'Name' => $theme['Name'], 'Version' => $theme['Version'], 'Status' => $theme['Status'], 'Title' => $theme['Title'], 'Author' => $theme['Author'], 'Author Name' => $theme['Author Name'], 'Author URI' => $theme['Author URI'], 'Description' => $theme['Description'], 'Template' => $theme['Template'], 'Stylesheet' => $theme['Stylesheet'], 'Template Files' => $theme['Template Files'], 'Stylesheet Files' => $theme['Stylesheet Files'], 'Template Dir' => $theme['Template Dir'], 'Stylesheet Dir' => $theme['Stylesheet Dir'], ] ); /* array( [Name] => Twenty Twenty-Three [Version] => 1.6 [Status] => publish [Title] => Twenty Twenty-Three [Author] => <a href="https://wordpress.org">the WordPress team</a> [Author Name] => the WordPress team [Author URI] => https://wordpress.org [Description] => Twenty Twenty-Three is designed to take advantage .... [Template] => twentytwentythree [Stylesheet] => twentytwentythree [Template Files] => Array( [patterns/call-to-action.php] => /home/wordpress/wp-content/themes/twentytwentythree/patterns/call-to-action.php [patterns/footer-default.php] => /home/wordpress/wp-content/themes/twentytwentythree/patterns/footer-default.php ) [Stylesheet Files] => Array( [style.css] => /home/wordpress/wp-content/themes/twentytwentythree/style.css ) [Template Dir] => /home/wordpress/wp-content/themes/twentytwentythree [Stylesheet Dir] => /home/wordpress/wp-content/themes/twentytwentythree ) */
Примеры
#1 Демонстрация
Давайте посмотрим, как выглядит получаемый объект. Используем функцию на дефолтной теме twentyfourteen:
print_r( wp_get_theme() );
Получим:
WP_Theme Object ( [theme_root:WP_Theme:private] => C:\sites\example.com\www/wp-content/themes [headers:WP_Theme:private] => Array ( [Name] => Twenty Fourteen [ThemeURI] => http://wordpress.org/themes/twentyfourteen [Description] => In 2014, our default theme lets you create a responsive magazine website with a sleek, modern design. Feature your favorite homepage content in either a grid or a slider. Use the three widget areas to customize your website, and change your content's layout with a full-width page template and a contributor page to show off your authors. Creating a magazine website with WordPress has never been easier. [Author] => the WordPress team [AuthorURI] => http://wordpress.org/ [Version] => 1.0 [Template] => [Status] => [Tags] => black, green, white, light, dark, two-columns, three-columns, left-sidebar, right-sidebar, fixed-layout, responsive-layout, custom-background, custom-header, custom-menu, editor-style, featured-images, flexible-header, full-width-template, microformats, post-formats, rtl-language-support, sticky-post, theme-options, translation-ready, accessibility-ready [TextDomain] => twentyfourteen [DomainPath] => ) [headers_sanitized:WP_Theme:private] => [name_translated:WP_Theme:private] => [errors:WP_Theme:private] => [stylesheet:WP_Theme:private] => twentyfourteen [template:WP_Theme:private] => twentyfourteen [parent:WP_Theme:private] => [theme_root_uri:WP_Theme:private] => [textdomain_loaded:WP_Theme:private] => [cache_hash:WP_Theme:private] => ea3ba1457a0fbfd275006de061bbffe5 )
#2 Выведем название текущей активной темы
echo wp_get_theme(); // Twenty Twenty-Three
#3 Выведем название установленной темы
$my_theme = wp_get_theme( 'twentyten' ); if ( $my_theme->exists() ) echo $my_theme;
#4 Выведем версию текущей темы
$my_theme = wp_get_theme(); echo $my_theme->get( 'Name' ) . " версия " . $my_theme->get( 'Version' );
#5 Выведем URL автора текущей темы
$my_theme = wp_get_theme(); echo $my_theme->get( 'AuthorURI' );
#6 Выведем другие данные текущей темы
$my_theme = wp_get_theme(); echo $my_theme->get( 'TextDomain' ); echo $my_theme->get( 'ThemeURI' );
Заметки
- Global. Строка[].
$wp_theme_directories
Список изменений
| С версии 3.4.0 | Введена. |
Код wp_get_theme() wp get theme WP 7.0
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, true ) ) {
$theme_root = WP_CONTENT_DIR . $theme_root;
}
}
return new WP_Theme( $stylesheet, $theme_root );
}