如何获得当前主题的路径?
2 个回答
- 投票数
-
- 2012-04-05
我认为您必须要小心一点,因为这取决于您要尝试执行的操作.
如果您使用的是子主题,
get_template_directory();
仍将转到父主题.但是,get_stylesheet_directory();
将转到当前主题(子主题或父主题).同样,这两个函数都返回绝对服务器路径.如果您想要一个完整的URI,用于链接或图像,则应出于说明的原因,使用正确的
get_template_directory_uri();
或get_stylesheet_directory_uri();
.摘要
-
get_stylesheet_directory()
:当前主题目录的文件路径 -
get_stylesheet_directory_uri()
:当前主题目录的网址路径 -
get_template_directory()
:父主题目录的文件路径 -
get_template_directory_uri()
:父主题目录的URL路径
I think you have to be a little careful because it depends on what you are trying to do.
If you are using a child theme
get_template_directory();
will still go to the parent theme. Howeverget_stylesheet_directory();
will go to the current theme, child or parent. Also, both these functions return absolute server paths.If you wanted a fully formed URI, for links or images, you should use
get_template_directory_uri();
orget_stylesheet_directory_uri();
using the correct one for the reasons stated.Summary
get_stylesheet_directory()
: file path to current Theme directoryget_stylesheet_directory_uri()
: url path to current Theme directoryget_template_directory()
: file path to parent Theme directoryget_template_directory_uri()
: url path to parent Theme directory
-
+1.*总是*使用`stylesheet`文件路径/URL来引用*当前*主题,并保留`模板`文件路径/URL来引用*父*主题.+1. *Always* use `stylesheet` filepath/url to reference the *current* Theme, and reserve `template` filepath/url to reference the *parent* Theme.
- 3
- 2012-04-05
- Chip Bennett
-
不幸的是,get_template_directory()返回整个服务器路径,如`/var/www/the/path/of/actual/wp-content/themes/mytheme`,如果WP通过FTP连接,则这不是您要使用$ wp_filesystem做的事情.Unfortunately get_template_directory() returns the entire server path like `/var/www/the/path/of/actual/wp-content/themes/mytheme` which is not what you want for doing stuff with $wp_filesystem if WP is connecting through FTP.
- 0
- 2014-09-11
- NoBugs
-
@NoBugs-我也正在获取整个服务器路径,但是使用get_stylesheet_directory_uri()而不是get_stylesheet_directory()解决了该问题.@NoBugs - I was getting the entire server path as well but using get_stylesheet_directory_uri() instead of get_stylesheet_directory() solved the issue.
- 0
- 2015-02-04
- TheLibzter
-
谢谢!我发现的大多数答案都可以通过使用get_stylesheet_directory()来解决,但后来我留在服务器的根目录中.get_stylesheet_directory_uri()是我祈祷的答案!Thank you! most answers i found got me part of the way there with using get_stylesheet_directory() but then I was left in the root directory of my server. get_stylesheet_directory_uri() was the answer to my prayers!
- 0
- 2020-08-07
- sigmapi13
-
- 2012-04-05
get_stylesheet_directory_uri
是您想要的.它返回当前主题的URL.get_stylesheet_directory
将返回服务器上的文件路径.例如:
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/image.png" />
如果您需要父主题,则
get_template_directory
和get_template_directory_uri
是等效的.如果没有父主题,则两者将返回相同的值.
进一步阅读:
-
get_stylesheet_directory
- 当前主题的绝对文件夹路径
- 例如
/var/www/yoursite/wp-content/themes/child_theme
-
get_template_directory
- 父主题的绝对文件夹路径
- 例如
/var/www/yoursite/wp-content/themes/parent_theme
-
get_stylesheet_directory_uri
- 当前主题的完整URL
- 例如
https://example.com/wp-content/themes/child_theme
-
get_template_directory_uri
- 父主题的完整URL
- 例如
https://example.com/wp-content/themes/parent_theme
get_stylesheet_directory_uri
is what you want. it returns the URL of the current theme.get_stylesheet_directory
will return the file path on the server.For example:
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/image.png" />
If you need the parent theme,
get_template_directory
andget_template_directory_uri
are the equivalents.If there is no parent theme then both will return the same value.
Further reading:
get_stylesheet_directory
- absolute folder path of current theme
- e.g.
/var/www/yoursite/wp-content/themes/child_theme
get_template_directory
- absolute folder path of parent theme
- e.g.
/var/www/yoursite/wp-content/themes/parent_theme
get_stylesheet_directory_uri
- full URL of current theme
- e.g.
https://example.com/wp-content/themes/child_theme
get_template_directory_uri
- full URL of parent theme
- e.g.
https://example.com/wp-content/themes/parent_theme
-
get_stylesheet_directory()是正确的答案.如果使用子主题,`get_template_directory()`将指向父主题.`get_stylesheet_directory()` is the correct answer. `get_template_directory()` will point to the parent theme if a child theme is being used.
- 0
- 2020-07-11
- Lucas Bustamante
-
我相应地调整了答案I adjusted the answer accordingly
- 0
- 2020-08-25
- Tom J Nowell
此代码用于获取当前插件的目录:
plugin_dir_url( __FILE__ )
.我应该使用什么来获取当前主题的目录?