Wordpress - 获取根目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16783218/
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
Wordpress - Get root directory?
提问by hrsetyono
How can I get the root directory of my site? I want to use it as the src
for <img>
.
如何获取我网站的根目录?我想用它作为src
for <img>
。
For example If I install the wordpress on www.mysite.com/blog/
, the root is /blog
.
例如,如果我在 上安装 wordpress www.mysite.com/blog/
,则根目录是/blog
.
But If I install it to www.mysite.com/test/blog
, the root is /test/blog
.
但是如果我把它安装到www.mysite.com/test/blog
,根/test/blog
。
I tried to use the snippet from this question, but it returns the whole path as seen in FTP. So instead of returning just /blog
, it returns /home/public_html/blog
which won't work if used as src
in image tag.
我试图使用这个问题中的片段,但它返回了在 FTP 中看到的整个路径。因此/blog
,它不仅返回 ,而且返回/home/public_html/blog
如果src
在图像标签中使用将不起作用。
Any solution? Thanks.
有什么解决办法吗?谢谢。
回答by mrówa
You may use site_url()
(eventually with echo
) to get absolute path with server name and directory. Also, have a look at wordpress documentationabout similar functions & what they provide.
您可以使用site_url()
(最终使用echo
)来获取服务器名称和目录的绝对路径。另外,请查看有关类似功能及其提供的内容的wordpress 文档。
回答by loeschg
You may have better luck over at the Wordpress Stack Exchangesite :)
您可能会在Wordpress Stack Exchange站点上获得更好的运气:)
And this suggestion to use ABSPATH
didn't help on that thread? https://stackoverflow.com/a/2356467/413254
而这个使用建议ABSPATH
对该线程没有帮助?https://stackoverflow.com/a/2356467/413254
回答by codingpuss
site_url() return the path with http, in some cases, it is not allowed if the server turns off url inclusion. You can use the function below to get the root path of wordpress installation folder:
site_url() 返回带有http的路径,在某些情况下,如果服务器关闭url包含是不允许的。您可以使用下面的函数来获取wordpress安装文件夹的根路径:
function get_wp_installation()
{
$full_path = getcwd();
$ar = explode("wp-", $full_path);
return $ar[0];
}