最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

include - if statement parent page for child pages

matteradmin5PV0评论

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
  • Welcome to the WordPress Development community, Wordpressy! Could you be a little more specific? In which file is your function located? Where is the test.php file? Where are you attempting to call your function? Do you receive any errors or warnings (is WP_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
Add a comment  | 

2 Answers 2

Reset to default 0

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

Post a comment

comment list (0)

  1. No comments so far