Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 从子主题根目录包含,或从父主题根目录包含_Wordpress_File_Include_Themes - Fatal编程技术网

Wordpress 从子主题根目录包含,或从父主题根目录包含

Wordpress 从子主题根目录包含,或从父主题根目录包含,wordpress,file,include,themes,Wordpress,File,Include,Themes,目前我正在开发一个带有functions.php的Wordpress主题,它将包含多个helper函数文件。我希望这个主题是非常灵活和可定制的,如果一个子主题是基于这个主题 我的functions.php中当前有这段代码: add_action( 'template_redirect', 'wpf_contact' ); /** * Include the logic/settings for tpl-contact.php. * * Includes the logic/settings

目前我正在开发一个带有functions.php的Wordpress主题,它将包含多个helper函数文件。我希望这个主题是非常灵活和可定制的,如果一个子主题是基于这个主题

我的functions.php中当前有这段代码:

add_action( 'template_redirect', 'wpf_contact' );
/**
 * Include the logic/settings for tpl-contact.php.
 *
 * Includes the logic/settings for tpl-contact.php. It will load the child's
 * mail.inc.php first if that is available.
 */
if ( ! function_exists( 'wpf_contact' ) ) {
    function wpf_contact() {

        if ( is_page_template( 'tpl-contact.php' ) && is_child_theme() && file_exists( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' ) ) {
            include( get_stylesheet_directory() . '/inc/wpf/mail.inc.php' );
        } else {
            include( get_template_directory() . '/inc/wpf/mail.inc.php');
        }

    } // end wpf_contact()
}
如果文件存在,上面的代码将从child加载mail.inc.php。如果它没有从父主题加载它

因为我有多个文件,我想这样加载。我在想可能没有更简单的方法了?WordPress中是否有内置函数可以实现这一点


或者我应该调整上述函数并添加参数,以便它也可以用于其他文件?不确定这是否有效。

要根据您的示例直接回答您的问题,我不会检查文件是否已放置在子主题中。如果子主题开发人员自定义了文件,他们可以简单地声明函数并包含自己的文件(无论放置在何处)。没有代码重复的风险,因为示例函数足够简单


即使为了将代码放在这里而简化了代码,您也可以始终将include包装在一个非常简单的函数中,该函数可以由子主题声明。这将使PHP保持高效运行,而不必一直检查文件。

这个问题属于.Tnx,我已将其添加到书签中!嗯,很有趣。我完全忽略了您可以简单地将函数添加到子主题function.php,谢谢!如果我没有在这里询问,我可能会使用一个函数,在每次使用子主题时检查文件是否存在。