更好的解决方案可能是按照
/* In your main plugin file */
if ( ! defined( \'YOUR_PLUGIN_ABSPATH\' ) ) {
define( \'YOUR_PLUGIN_ABSPATH\', dirname( __FILE__ ) );
}
/* In any file of the plugin */
if( is_front_page() ) {
/* adjust path if file.php is in a subfolder */
require_once ( YOUR_PLUGIN_ABSPATH . \'/file.php\' );
}
如果仍然需要处理完整路径,则它们位于文件中。php。
Additional Option
此外,使用条件标记可能不是最好的主意(
if( is_front_page() )
). 或者,您可以让插件生成一个短代码,以输出所需的标记。
这将有两个好处:第一,它很容易放置和移动到您的(首页)中。另一方面,您可以在任何地方使用它,而无需修改代码。
/* In your main plugin file */
if ( ! defined( \'YOUR_PLUGIN_ABSPATH\' ) ) {
define( \'YOUR_PLUGIN_ABSPATH\', dirname( __FILE__ ) );
}
/* In any file of the plugin */
function your_include( $atts ) {
/* "path" is a shortcode attribute, you can use it to include several files */
extract( shortcode_atts(
array(
\'path\' => \'file.php\'
),
$atts ) );
require_once ( YOUR_PLUGIN_ABSPATH . \'/\' . $path );
/**
* file.php should be adjusted to save whatever you were echoing in before
* in a variable (in this example $output), which is returned
* by the shortcode function
*/
return $output
}
add_shortcode( \'your-include\', \'your_include\' );