如何检查主题是否处于活动状态?


11

我想检查一下十二十二主题是否处于活动状态。我知道如果我正在检查一个活动的插件,我会做类似的事情:

$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
if ( in_array( 'plugin-folder/plugin-folder.php', $active_plugins ) ) {
    //do stuff
} else {
add_action( 'admin_notices', 'create-a-notice' );
}

检查主题是否处于活动状态以便我可以为该主题运行功能的正确方法是什么?


Answers:


22

您可以使用wp_get_theme

<?php
$theme = wp_get_theme(); // gets the current theme
if ( 'Twenty Twelve' == $theme->name || 'Twenty Twelve' == $theme->parent_theme ) {
    // if you're here Twenty Twelve is the active theme or is
    // the current theme's parent theme
}

或者,您可以简单地检查二十二分之一的函数是否存在-可靠性可能较低;例如,一个插件甚至另一个主题都可以声明twentytwelve_setup

<?php
if ( function_exists( 'twentytwelve_setup' ) ) {
   // Twenty Twelve is the current theme or the active theme's parent.
}

5
  if( 'twentytwelve' == get_option( 'template' ) ) {
    // do something
  }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.