php 在 Wordpress 中获取 Wordpress 子主题路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20059811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Wordpress Child Theme Path in Wordpress
提问by user1048676
I have a parent and child theme. In my child theme I declared a variable CHILD_DIR so I can add custom JS and CSS files to the my child theme's folder structure. I did this in my functions.php file in my child theme by doing this:
我有一个父子主题。在我的子主题中,我声明了一个变量 CHILD_DIR,因此我可以将自定义 JS 和 CSS 文件添加到我的子主题的文件夹结构中。我通过这样做在我的子主题的functions.php文件中做到了这一点:
define( 'CHILD_DIR', get_stylesheet_directory_uri() );
This produces a URL which is what I want since I'm using it like this:
这会产生一个我想要的 URL,因为我是这样使用它的:
wp_enqueue_script('custom', CHILD_DIR .'/custom.js', array( 'jquery' ));
I also added some additional folders to my child theme which I want to use within a Wordpress page. I tried to do something like this:
我还在我的子主题中添加了一些额外的文件夹,我想在 Wordpress 页面中使用这些文件夹。我试图做这样的事情:
require_once CHILD_DIR . '/Zend/Loader.php';
I get an error since it's trying to provide a URL and I want a menu path to include the PHP and not try and make it an HTTP request. How can I define a path for the PHP instead of a URL to default to my child theme?
我收到一个错误,因为它试图提供一个 URL,我想要一个包含 PHP 的菜单路径,而不是尝试让它成为 HTTP 请求。如何为 PHP 定义路径而不是默认为我的子主题的 URL?
回答by brasofilo
It's almost the same functions:
它的功能几乎相同:
- Parent theme:
get_template_directory() - Parent theme or child theme:
get_stylesheet_directory()
- 父主题:
get_template_directory() - 父主题或子主题:
get_stylesheet_directory()
回答by Mohammed Abrar Ahmed
Main theme path : get_template_directory_uri();
主主题路径:get_template_directory_uri();
Child Theme path : get_stylesheet_directory_uri();or get_stylesheet_uri();
子主题路径:get_stylesheet_directory_uri(); 或get_stylesheet_uri();
NOTE : Irrespective of calling js files or css files, For gettig the path of the theme or the path of the child theme we have to use the above functions.
注意:无论是调用js文件还是css文件,gettig主题的路径或者子主题的路径我们都必须使用上面的函数。
回答by Blackjoker
For Retrieve Theme stylesheet directory URL:
对于检索主题样式表目录 URL:
get_theme_file_uri() oR
get_stylesheet_directory_uri()
For Retrieve Absolute File Paths:
对于检索绝对文件路径:
get_stylesheet_directory() oR
get_theme_file_uri()

