函数文档

get_template()

💡 云策文档标注

概述

get_template() 函数用于获取当前活动主题的目录名称,返回字符串类型。当使用子主题时,此函数返回父主题的目录名。

关键要点

  • 返回当前活动主题的目录名,不带尾部斜杠
  • 如果使用子主题,返回父主题的目录名;要获取子主题目录名,请使用 get_stylesheet()
  • 函数通过 apply_filters('template', $template) 应用过滤器,允许开发者修改返回值

代码示例

echo get_template(); // 输出:heli

注意事项

  • 在子主题激活时,get_template() 返回父主题名,而非子主题名;如需获取当前主题名(无论是否为子主题),请使用 get_stylesheet()

📄 原文内容

Retrieves name of the active theme.

Return

string Template name.

More Information

This function retrieves the directory name of the current theme, without the trailing slash. In the case a child theme is being used, the directory name of the parent theme will be returned. Use get_stylesheet() to get the directory name of the child theme.

Source

function get_template() {
	/**
	 * Filters the name of the active theme.
	 *
	 * @since 1.5.0
	 *
	 * @param string $template active theme's directory name.
	 */
	return apply_filters( 'template', get_option( 'template' ) );
}

Hooks

apply_filters( ‘template’, string $template )

Filters the name of the active theme.

Changelog

Version Description
1.5.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Note that get_template will return the parent theme’s name, NOT the child theme’s name, if a child theme is currently activated.

    If you wish to obtain the current theme regardless if it is a child theme or the parent theme, use get_stylesheet

    (reference question on wordpress stack exchange)