views:

1421

answers:

3

Hi - Working on my first Wordpress site so I'm sure this is a really basic question. I'm struggling to write a conditional php statement that performs a certain action when a page is a child of a parent page.

For example, rather than just specifying one page, as below, I'd like to specify all pages that have the 'About Us' page as a parent:

<?php if (is_page('About Us')) echo 'Hello World!'; ?>

I've tried the "child_of" function but it wasn't as straightforward as I'd hoped.

When I use the below, I get a syntax error - probably just me not knowing how to use the function:

<?php if (child_of('About Us')) echo 'Hello World!'; ?>

Any suggestions? Thanks!!

+1  A: 

You're getting an error because there is no such thing as a child_of() function in WordPress.

child_of() is a way of searching using the get_pages() function.

$pages = get_pages('child_of=##');

where ## is the numeric ID (not the name) of the 'About us' page.

ceejayoz
Awesome - I figured I was using it incorrectly - thank you for clearing that up!I also just got it to work this way in case anyone else is struggling and wants some options: <?php if ($post->post_parent = 'About Us') echo 'Hello World!'; ?>Thanks again!
Erg... nevermind that didn't work. It's assuming the first if statement in the list in all cases.
A: 

This is the final code that worked for me - not sure if it's the proper way to do what I'm trying to do, but will post for the benefit of anyone with my same question.

I have a set of right hand columns, each specific to a section of the site (each parent page representing a section of the site).

I want the parent page and all of that parent's child pages to pull the same specific right hand column.

Here's what I did:

<?php 

if (is_page('Page Name 1') || $post->post_parent == '##') {
include (TEMPLATEPATH . '/right-1.php');

} elseif (is_page('Page Name 2') || $post->post_parent == '##') {
include (TEMPLATEPATH . '/right-2.php');

} elseif (is_page('Page Name 3') || $post->post_parent == '##') {
include (TEMPLATEPATH . '/right-3.php');

} elseif (is_page('Page Name 4') || $post->post_parent == '##')
include (TEMPLATEPATH . '/right-4.php');

?>

Where the ## represents the ID # of the page.

A: 

Add the following function to your functions.php theme file:

function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
    global $post;         // load details about this page
    $anc = get_post_ancestors( $post->ID );
    foreach($anc as $ancestor) {
        if(is_page() && $ancestor == $pid) {
            return true;
        }
    }
    if(is_page()&&(is_page($pid))) 
               return true;   // we're at the page or at a sub page
    else 
               return false;  // we're elsewhere
};

Then you can use the following:

if(is_tree('2')){ // 2 being the parent page id
   // Do something if the parent page of the current page has the id of two
}

Reference: http://codex.wordpress.org/Conditional_Tags

Danny Croft