get_parent_theme_file_uri()
云策文档标注
概述
get_parent_theme_file_uri() 函数用于获取父主题中指定文件的 URL。它接受一个可选的文件路径参数,并返回完整的 URL 字符串。
关键要点
- 函数参数 $file 为可选字符串,指定要获取 URL 的文件路径,默认为空。
- 返回值为字符串,表示文件的完整 URL。
- 内部实现基于 get_template_directory_uri() 函数构建 URL。
- 提供 'parent_theme_file_uri' 过滤器钩子,允许开发者自定义 URL 输出。
- 自 WordPress 4.7.0 版本引入。
代码示例
function get_parent_theme_file_uri( $file = '' ) {
$file = ltrim( $file, '/' );
if ( empty( $file ) ) {
$url = get_template_directory_uri();
} else {
$url = get_template_directory_uri() . '/' . $file;
}
/**
* Filters the URL to a file in the parent theme.
*
* @since 4.7.0
*
* @param string $url The file URL.
* @param string $file The requested file to search for.
*/
return apply_filters( 'parent_theme_file_uri', $url, $file );
}注意事项
- 函数处理文件路径时,会使用 ltrim() 移除开头的斜杠,确保 URL 拼接正确。
- 当 $file 参数为空时,返回父主题目录的根 URL。
- 相关函数包括 get_template_directory_uri() 和 apply_filters(),用于获取模板目录 URI 和应用过滤器。
原文内容
Retrieves the URL of a file in the parent theme.
Parameters
$filestringoptional-
File to return the URL for in the template directory.
Source
function get_parent_theme_file_uri( $file = '' ) {
$file = ltrim( $file, '/' );
if ( empty( $file ) ) {
$url = get_template_directory_uri();
} else {
$url = get_template_directory_uri() . '/' . $file;
}
/**
* Filters the URL to a file in the parent theme.
*
* @since 4.7.0
*
* @param string $url The file URL.
* @param string $file The requested file to search for.
*/
return apply_filters( 'parent_theme_file_uri', $url, $file );
}
Hooks
- apply_filters( ‘parent_theme_file_uri’, string $url, string $file )
-
Filters the URL to a file in the parent theme.
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |