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.
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