简体   繁体   English

"在 functions.php WordPress 中包含外部文件"

[英]Include external files in functions.php WordPress

In my WordPress I have a functions.php file containing 1500 lines of code.在我的 WordPress 中,我有一个包含 1500 行代码的 functions.php 文件。

Most of this code comes from a program I wrote that connects to an API and sends data from WordPress to it.大部分代码来自我编写的连接到 API 并将数据从 WordPress 发送到它的程序。 I am not a WordPress developer, so I do not know if this is possible.我不是 WordPress 开发人员,所以我不知道这是否可能。 Can you make a different file to keep functions.php clean and then include the external file inside the functions.php?你可以制作一个不同的文件来保持functions.php的干净,然后将外部文件包含在functions.php中吗?

"

Yes you can include the external file in functions.php是的,您可以在 functions.php 中包含外部文件

require_once( get_template_directory() . 'anyfilename.php' ); require_once( get_template_directory() . 'anyfilename.php' );

If you are working with a Child Theme but the path of your include route is not found:如果您正在使用子主题但未找到包含路径的路径:

Try the following:请尝试以下操作:

include_once( get_stylesheet_directory() .'/xxxx/anyfilename.php');

Check get_stylesheet_directory() to see your child theme path.检查get_stylesheet_directory()以查看您的子主题路径。

You can include external files in functions.php by using either您可以使用以下任一方法在 functions.php 中包含外部文件

require_once();

or else you can use否则你可以使用

include_once();

The difference between these two is that in the event that the file is not found require will give out a fatal error and then stop executing the rest of the file where as include will only issue a warning but will continue the execution of the file.这两者之间的区别在于,如果找不到文件, require 将发出致命错误,然后停止执行文件的其余部分,而 include 只会发出警告,但会继续执行文件。

One very important suggestion:一个非常重要的建议:

If you want to include another PHP code into your PHP file, your included file must start with <?php<\/code> .如果您想在您的 PHP 文件中包含另一个 PHP 代码,您包含的文件必须以<?php<\/code>开头。 Ending tag is not needed.不需要结束标签。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM