如何获取活动主题的弹头?

时间:2016-03-17 作者:henrywright

我可以使用wp_get_theme(). 例如:

$theme = wp_get_theme();
echo $theme->get( \'TextDomain\' ); // twentyfifteen
echo $theme->get( \'ThemeURI\' ); // https://wordpress.org/themes/twentyfifteen/
有没有办法得到主题的slug?在这种情况下,应该是二十一五。请注意,主题的slug并不总是与主题的文本域相同。如果可能的话,我还希望避免在主题的URL上执行字符串替换。

参考号:https://codex.wordpress.org/Function_Reference/wp_get_theme

6 个回复
最合适的回答,由SO网友:henrywright 整理而成

我发现最接近主题slug的是主题的目录名。可以使用get_template():

echo get_template(); // twentyfifteen
参考号:https://codex.wordpress.org/Function_Reference/get_template

SO网友:RRikesh

你可以在options 表,存储在名称下stylesheet.

echo get_option(\'stylesheet\');

SO网友:drdogbot7

Short Answer: get_stylesheet();

从技术上讲,主题没有“slug”值。给定主题目录的名称就是您想要的名称。

get_template();
…将返回主题的目录名,or the parent theme 如果当前主题是子主题。

get_option(\'stylesheet\');
将始终返回活动主题的目录名,无论它是否为子主题。

get_stylesheet();
将始终返回活动主题的目录名,无论它是否为子主题。此函数本质上是get_option(\'stylesheet\');, 除此之外,它还应用“样式表”过滤器。

function get_stylesheet() {
/**
 * Filters the name of current stylesheet.
 *
 * @since 1.5.0
 *
 * @param string $stylesheet Name of the current stylesheet.
 */
return apply_filters( \'stylesheet\', get_option( \'stylesheet\' ) );
}
我不确定“样式表”过滤器的作用。看起来可能与自定义程序有关。

在绝大多数情况下,这三个功能会做同样的事情,但是get_stylesheet(); 似乎是最安全的赌注。

SO网友:Mahdi Yazdani

wp_get_theme 获取WP_Theme 主题的对象。

$theme = wp_get_theme();

if ( \'Conj\' === $theme->name || \'conj\' === $theme->template ) {
    // Do something...
}

SO网友:ihsanberahim

wp_get_active_and_valid_themes()
我在wp设置中找到了这个。php

// Load the functions for the active theme, for both parent and child theme if applicable.
foreach ( wp_get_active_and_valid_themes() as $theme ) {
    if ( file_exists( $theme . \'/functions.php\' ) ) {
        include $theme . \'/functions.php\';
    }
}

SO网友:Bhavesh Nariya

Yo can get it by get_template_directory_uri()