admin管理员组文章数量:1433514
im trying to achieve a code to include a file into pages, these pages has a parent page. so instead of repeating the code of the "if statement" i use this function but doesn't work. i want any page falls under that parent page includes that test.php file
global $post;
if ($post->post_parent == 974) {
include 'test.php';
}
Thank you
im trying to achieve a code to include a file into pages, these pages has a parent page. so instead of repeating the code of the "if statement" i use this function but doesn't work. i want any page falls under that parent page includes that test.php file
global $post;
if ($post->post_parent == 974) {
include 'test.php';
}
Thank you
Share Improve this question asked Jan 27, 2015 at 0:06 WordpressyWordpressy 32 bronze badges 1 |2 Answers
Reset to default 0If test.php is in your theme directory, try locate_template('test.php') instead.
If it is somewhere else, make sure that you are trying to include it properly via the path needed.
Just doing include 'test.php' most likely makes it think that test.php is in the root directory of the site as /index.php runs the site (e.g. where your wp_config.php file is unless you have a custom set up.)
Most likely you are simply looking in the wrong place for the file to include.
Try:
require 'test.php';
If the page dies, it is trying to load, but looking in the wrong place.
i want any page falls under that parent page includes that test.php file
Sound like you need the is_tree()
function. I've used it several times for this type of functionality. First you would declare this function in your functions.php file:
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
if(is_page()&&($post->post_parent==$pid||is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
Then, you would use the function wherever you needed that condition to be met, and use the parent page ID as the argument:
if (is_tree(974)) {
include 'test.php';
}
You can see this method in detail over at CSS-Tricks.
EDIT: This is all assuming you are following proper themeing conventions.
本文标签: includeif statement parent page for child pages
版权声明:本文标题:include - if statement parent page for child pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745604488a2665759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
test.php
file? Where are you attempting to call your function? Do you receive any errors or warnings (isWP_DEBUG
enabled?). Please read the How to Ask section of our help center for more information regarding what makes a good question. – bosco Commented Jan 27, 2015 at 1:04