stylesheet_uri
云策文档标注
概述
stylesheet_uri 是一个 WordPress 过滤器,用于修改活动主题或子主题的样式表 URI。开发者可以通过此 Hook 动态调整样式表路径,例如基于页面条件加载自定义样式。
关键要点
- 过滤器名称:stylesheet_uri
- 参数:$stylesheet_uri(样式表 URI 字符串)和 $stylesheet_dir_uri(样式表目录 URI 字符串)
- 用途:允许在运行时修改活动主题的样式表 URI,常用于条件加载或替换样式文件
- 相关函数:get_stylesheet_uri() 用于检索样式表 URI
- 引入版本:WordPress 1.5.0
代码示例
/**
* Use a custom stylesheet for single posts.
*
* @param string $stylesheet Original stylesheet URI.
* @param string $stylesheet_dir Stylesheet directory.
* @return string (Maybe modified) stylesheet URI.
*/
function wpdocs_custom_stylesheet_for_single_posts( $stylesheet, $stylesheet_dir ) {
if ( is_singular( 'post' ) ) {
$stylesheet = $stylesheet_dir . '/custom-single.css';
}
return $stylesheet;
}
add_filter( 'stylesheet_uri', 'wpdocs_custom_stylesheet_for_single_posts', 10, 2 );
原文内容
Filters the URI of the active theme stylesheet.
Parameters
$stylesheet_uristring-
Stylesheet URI for the active theme/child theme.
$stylesheet_dir_uristring-
Stylesheet directory URI for the active theme/child theme.
Source
return apply_filters( 'stylesheet_uri', $stylesheet_uri, $stylesheet_dir_uri );
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |
Skip to note 2 content
WP SITES
Use a custom stylesheet for single posts.
/** * Use a custom stylesheet for single posts. * * @param string $stylesheet Original stylesheet URI. * @param string $stylesheet_dir Stylesheet directory. * @return string (Maybe modified) stylesheet URI. */ function wpdocs_custom_stylesheet_for_single_posts( $stylesheet, $stylesheet_dir ) { if ( is_singular( 'post' ) ) { $stylesheet = $stylesheet_dir . '/custom-single.css'; } return $stylesheet; } add_filter( 'stylesheet_uri', 'wpdocs_custom_stylesheet_for_single_posts', 10, 2 );