Vas*_*isJ 2 php wordpress include
我试图在另一个文件中包含一个文件,但没有。没有错误。首先,我包含了我的主文件。
include('inc/onefolder/mymainfile.php');
Run Code Online (Sandbox Code Playgroud)
然后我尝试在此文件 ( mymainfile.php) 中包含辅助文件。
include('inc/onefolder/anotherfolder/secondaryfile.php');
Run Code Online (Sandbox Code Playgroud)
当您使用 PHPinclude或requireWordPress 插件时,最好使用 WordPress 特定功能来获取文件的绝对路径。您可以使用plugin_dir_path:
include( plugin_dir_path( __FILE__ ) . 'inc/onefolder/mymainfile.php');
Run Code Online (Sandbox Code Playgroud)
插件目录并不总是在 wp-content/plugins ,因为我们可以在WordPress Codex 中阅读:
重要的是要记住 WordPress 允许用户将他们的 wp-content 目录放在他们想要的任何地方,所以你永远不要假设插件将在 wp-content/plugins 中,或者上传将在 wp-content/uploads 中,或者主题将在 wp-content/themes 中。
因此,使用 plugin_dir_path() 可确保include始终指向正确的路径。