ABSPATH还是__FILE__? [英] ABSPATH or __FILE__?
问题描述
有人可以告诉我这两种方法是否都比其他方法有优势,为什么?
Can someone tell me if either of these two methods has an advantage over the other and why?
$mydir = ABSPATH.'/wp-content/themes/mytheme/images';
$mydir = dirname(__FILE__).'/images';
它们既可以用于获取"mytheme"图像目录的绝对路径,也可以用于将wordpress安装在根目录还是根目录之外的子目录中的结构.在这两种情况下,都可以从"mytheme"文件夹下的functions.php文件中调用它们.
They can both be used to obtain and absolute path to the images directory of "mytheme" regardless of structure of whether wordpress is installed on the root directory or in a subdirectory off the root. In both cases, they are being called from the functions.php file which is located under the "mytheme" folder.
推荐答案
我个人更喜欢dirname()
,因为它始终可以确保为我提供正确的结果,而ABSPATH方法依赖于固定的主题路径和主题名称,都可以改变.
I would personally prefer dirname()
as it is always guaranteed to give me the correct result, while the ABSPATH method relies on a fixed theme path and theme name that can both change.
顺便说一句,您可以使用__DIR__
代替dirname(__FILE__)
.
By the way, you can use __DIR__
instead of dirname(__FILE__)
.
这篇关于ABSPATH还是__FILE__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!