问题
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?
回答1:
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.
回答2:
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
回答3:
Simple solution where you want to link to the parent page from the child page; $post->post_parent holds the ID of a page's parent if there is one or it is zero if there is not one:
<?php
if($post->post_parent !== 0){
print '<a href="'.get_permalink($post->post_parent).'">← Back</a>';
}
?>
so for this case you would want to change the if() to check if $post->post_parent == $id_of_about_page.
回答4:
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.
回答5:
This code solved the problem for me guys, incase any one is wondering for a alernative:
<?php
global $post;
if ($post->post_parent == 9) {
echo 'blah blah';
}; ?>
"9" is the id of the parent page.
来源:https://stackoverflow.com/questions/891238/wordpress-php-way-to-test-if-pages-are-children-of-a-parent-page