指向父主题而不是子主题的get_template_directory_uri
2 个回答
- 投票数
-
- 2016-06-18
get_template_directory_uri()
将始终返回当前父主题的URI.要获取子主题URI,您需要使用
get_stylesheet_directory_uri()
.您可以在文档中找到这些 ,以及用于获取各种主题目录位置的其他有用功能的列表.
如果您喜欢使用常量,那么
TEMPLATEPATH
类似于调用get_template_directory()
(即父主题)和STYLESHEETPATH
类似于调用get_stylesheet_directory()
(即子主题).这些常量是由WordPress核心在
wp-includes/default-constants.php
中设置的,基本上是这样的:define('TEMPLATEPATH', get_template_directory()); ... define('STYLESHEETPATH', get_stylesheet_directory());
如果没有子主题,则"模板" 和"样式表"功能都将返回父主题位置.
请注意这些函数与以
_uri
结尾的函数之间的区别-它们将返回服务器的绝对路径(例如/home/example/public_html/wp-content/yourtheme
),而_uri
函数将返回公共地址(即URL)-例如.http://example.com/wp-content/themes/yourtheme
.get_template_directory_uri()
will always return the URI of the current parent theme.To get the child theme URI instead, you need to use
get_stylesheet_directory_uri()
.You can find these in the documentation, along with a list of other useful functions for getting various theme directory locations.
If you prefer to use a constant, then
TEMPLATEPATH
is akin to callingget_template_directory()
(i.e. the parent theme), andSTYLESHEETPATH
is akin to callingget_stylesheet_directory()
(i.e. the child theme).These constants are set by WordPress core in
wp-includes/default-constants.php
and basically look like this:define('TEMPLATEPATH', get_template_directory()); ... define('STYLESHEETPATH', get_stylesheet_directory());
If there is no child theme, then both the 'template' and 'stylesheet' functions will return the parent theme location.
Note the difference between these functions and the functions ending in
_uri
- these will return the absolute server path (eg./home/example/public_html/wp-content/yourtheme
), whereas the_uri
functions will return the public address (aka URL) - eg.http://example.com/wp-content/themes/yourtheme
.-
怎么样包括(TEMPLATEPATH.'/myGallery/gallery_functions_include.php');这个也转到父目录what about include (TEMPLATEPATH . '/myGallery/gallery_functions_include.php'); this one also goes to the parent directory
- 0
- 2016-06-18
- Elroy Fernandes
-
@ElroyFernandes我已将此添加到我的答案中.STYLESHEETPATH是您想要的常量@ElroyFernandes I've added this to my answer. STYLESHEETPATH is the constant you'd want instead
- 0
- 2016-06-18
- Tim Malone
-
感谢您回答问题,而不仅仅是说RTM.这首先出现在我的搜索结果中.Thanks for answering the question instead of just saying RTM. This popped up first in my search results.
- 2
- 2017-05-04
- rinogo
-
答案不错,但WordPress的命名却不好-它不仅用于样式表,还用于JS,资产,包含等.Good answer but bad naming on WordPress's part - it's not just for stylesheets, it's for JS, assets, includes etc.
- 2
- 2018-09-21
- Paul Feakins
-
@PaulFeakins不要开始在WordPress中命名不一致-这是一条漫长而多风的道路,导致人们知道哪里去了!;)@PaulFeakins Don’t get started on naming inconsistencies in WordPress - that’s a long and windy road that leads who-knows-where! ;)
- 1
- 2018-09-21
- Tim Malone
-
- 2018-06-06
您应该将自定义模板(不受活动主题控制的模板)移动到子文件夹.
将主题与所有自定义文件分开,这样可以在不丢失自定义工作的情况下更新主题.
您现成的主题就在这里 ------------------------------------ \\ Site \ wp-content \themes \ some_theme
您的孩子主题住在这里 --------------------------- \\ Site \ wp-content \themes \ some_theme-child
您的自定义样式和模板以及所有包含的内容(如自定义javascript,未保存到WP的图像,自定义字体,json数据文件以及您可能会排队的任何插件)应移至主题的子文件夹之外
\themes \ some_theme \themes \ some_theme-child \(此处是您所有的自定义php模板文件) \themes \ some_theme-child \images \themes \ some_theme-child \includes \themes \ some_theme-child \语言 \themes \ some_theme-child \json \themes \ some_theme-child \ style
对于您的自定义样式页面(不是主题的重写style.css ), wp_enqueue_style('some-css',get_stylesheet_directory().'/style/some.css ',false,'0.0.1','all');
使用get_stylesheet_directory_uri()与您的xhr调用等.
You should move your custom templates, those that are not controlled by the active theme, to a child folder.
Keep the theme separate from all customized files this way the theme can be updated without losing your custom work.
Your out-of-the-box theme lives here ------------------------------------ \\Site\wp-content\themes\some_theme
Your child theme lives here --------------------------- \\Site\wp-content\themes\some_theme-child
Your custom styles and templates and all your includes (things like custom javascript, images that are not saved to WP, custom fonts, json data files, and any plugins that you might enqueue) should be moved to child folder OUTSIDE of theme.
\themes\some_theme \themes\some_theme-child\ (all your custom php template files here) \themes\some_theme-child\images \themes\some_theme-child\includes \themes\some_theme-child\languages \themes\some_theme-child\json \themes\some_theme-child\style
For your custom style pages (not the theme's overridden style.css) enqueue with wp_enqueue_style( 'some-css', get_stylesheet_directory() . '/style/some.css' , false, '0.0.1', 'all');
Use get_stylesheet_directory_uri() with your xhr calls, etc.
我遇到的问题是get_template_directory_uri指向父主题,例如
site/wp-content/themes/twentythirteen/myGallery/gallery_functions_include.php
但我希望它指向我的子主题,该主题应为
site/wp-content/themes/child-twentythirteen/myGallery/gallery_functions_include.php
我正在使用的是
include (TEMPLATEPATH . '/myGallery/gallery_functions_include.php');