admin管理员组文章数量:1022560
I have created a custom file that both the header and the footer needs to render certain content.
If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the header.php
file it works as expected BUT only in the header.
Likewise with the footer, If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the footer.php
file it works as expected BUT only in the footer.
The moment I have the include on both files, the page renders up until the footer starts and then stops rendering.
How can I include this file so it can be accessed from anywhere in the theme?
I also tried the other import methods in PHP - require, require_once, and include_once – same deal.
Thank you in advance.
I have created a custom file that both the header and the footer needs to render certain content.
If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the header.php
file it works as expected BUT only in the header.
Likewise with the footer, If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the footer.php
file it works as expected BUT only in the footer.
The moment I have the include on both files, the page renders up until the footer starts and then stops rendering.
How can I include this file so it can be accessed from anywhere in the theme?
I also tried the other import methods in PHP - require, require_once, and include_once – same deal.
Thank you in advance.
Share Improve this question asked May 13, 2019 at 6:49 SergioSergio 1037 bronze badges 2 |1 Answer
Reset to default 1You wrote that you have a function definition in file that you include. I suspect that the reason is the function redeclaration error.
You should move the function definition from myCustomFile.php
to functions.php
.
You can also before each function definition in myCustomFile.php
check whether there is already a function of the same name.
if ( ! function_exists('you_function_name') ) {
function you_function_name() {
// ...
}
}
I have created a custom file that both the header and the footer needs to render certain content.
If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the header.php
file it works as expected BUT only in the header.
Likewise with the footer, If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the footer.php
file it works as expected BUT only in the footer.
The moment I have the include on both files, the page renders up until the footer starts and then stops rendering.
How can I include this file so it can be accessed from anywhere in the theme?
I also tried the other import methods in PHP - require, require_once, and include_once – same deal.
Thank you in advance.
I have created a custom file that both the header and the footer needs to render certain content.
If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the header.php
file it works as expected BUT only in the header.
Likewise with the footer, If I only do include($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/myTheme/myCustomFile.php');
in the footer.php
file it works as expected BUT only in the footer.
The moment I have the include on both files, the page renders up until the footer starts and then stops rendering.
How can I include this file so it can be accessed from anywhere in the theme?
I also tried the other import methods in PHP - require, require_once, and include_once – same deal.
Thank you in advance.
Share Improve this question asked May 13, 2019 at 6:49 SergioSergio 1037 bronze badges 2-
myCustomFile.php
is in theme directory, so just addinclude 'myCustomFile.php';
in both files (footer and header). Do you have function or class definitions in included file? – nmr Commented May 13, 2019 at 6:59 -
@nmr thanks, but still not working. Same thing is happening, the page renders fine up until the footer and then stops rendering. But the header renders the information from
myCustomFile.php
fine. I do have functions in the custom file, but I don't think they are interfering because then it would not render on the header. – Sergio Commented May 13, 2019 at 7:05
1 Answer
Reset to default 1You wrote that you have a function definition in file that you include. I suspect that the reason is the function redeclaration error.
You should move the function definition from myCustomFile.php
to functions.php
.
You can also before each function definition in myCustomFile.php
check whether there is already a function of the same name.
if ( ! function_exists('you_function_name') ) {
function you_function_name() {
// ...
}
}
本文标签: customizationHow to include custom PHP file both in header and footer files
版权声明:本文标题:customization - How to include custom PHP file both in header and footer files 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745500172a2153353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
myCustomFile.php
is in theme directory, so just addinclude 'myCustomFile.php';
in both files (footer and header). Do you have function or class definitions in included file? – nmr Commented May 13, 2019 at 6:59myCustomFile.php
fine. I do have functions in the custom file, but I don't think they are interfering because then it would not render on the header. – Sergio Commented May 13, 2019 at 7:05