get_stylesheet_uri()
云策文档标注
概述
get_stylesheet_uri() 函数用于获取当前活动主题的样式表 URI,即 style.css 文件的完整 URL。它基于 get_stylesheet_directory_uri() 构建,并可通过过滤器进行修改。
关键要点
- 返回当前活动主题(包括子主题)的 style.css 文件 URI
- 内部调用 get_stylesheet_directory_uri() 并拼接 '/style.css'
- 提供 'stylesheet_uri' 过滤器,允许开发者自定义输出
- 返回类型为字符串,可直接用于链接或输出
代码示例
function get_stylesheet_uri() {
$stylesheet_dir_uri = get_stylesheet_directory_uri();
$stylesheet_uri = $stylesheet_dir_uri . '/style.css';
/**
* Filters the URI of the active theme stylesheet.
*
* @since 1.5.0
*
* @param string $stylesheet_uri Stylesheet URI for the active theme/child theme.
* @param string $stylesheet_dir_uri Stylesheet directory URI for the active theme/child theme.
*/
return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
}注意事项
- 自 WordPress 1.5.0 版本引入,兼容性良好
- 适用于父主题和子主题场景,自动处理路径差异
- 相关函数包括 get_stylesheet_directory_uri() 和 get_bloginfo()
原文内容
Retrieves stylesheet URI for the active theme.
Description
The stylesheet file name is ‘style.css’ which is appended to the stylesheet directory URI path.
See get_stylesheet_directory_uri() .
Source
function get_stylesheet_uri() {
$stylesheet_dir_uri = get_stylesheet_directory_uri();
$stylesheet_uri = $stylesheet_dir_uri . '/style.css';
/**
* Filters the URI of the active theme stylesheet.
*
* @since 1.5.0
*
* @param string $stylesheet_uri Stylesheet URI for the active theme/child theme.
* @param string $stylesheet_dir_uri Stylesheet directory URI for the active theme/child theme.
*/
return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
}
Hooks
- apply_filters( ‘stylesheet_uri’, string $stylesheet_uri, string $stylesheet_dir_uri )
-
Filters the URI of the active theme stylesheet.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 3 content
Codex
To output the URL
Skip to note 4 content
Mwale Kalenga
Example output
When using a parent theme:
https://example.com/wp-content/themes/<strong>parent-theme</strong>/style.cssWhen using a child theme:
https://example.com/wp-content/themes/<strong>child-theme</strong>/style.css