WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to write a condition for a subpage...in other words

if we're on the subpage "duck" then do something...if not do something else.

I found some code I thought would work, but it shows up on all of the pages under a given parent page, not just on its individual page...I'm only trying to target the individual page.

This goes in functions:

// Subpage Shortcode
function is_child($page_id_or_slug) {
    global $post;
    if(!is_int($page_id_or_slug)) {
        $page = get_page_by_path($page_id_or_slug);
        $page_id_or_slug = $page->ID;
    if(is_page() && $post->post_parent == $page_id_or_slug ) {
        return true;
    } else {
        return false;

This is my if:

if (is_child("Youth")) {
    echo "duck";
} else {
    echo "something else";


share|improve this question

Hmm, I'll take a crack at this. There's a few ways to go about this. Check to see if page is a direct child, check if it is an ancestor.

Something like below might work for you

if (283 == $post->post_parent) {
echo 'this post has a parent of 283 so do this';
}else {
    echo "or do something else";
share|improve this answer
Alex, I have more than one child page...which is why the current code doesn't work...is there any way to make your code work for multiple subpages? – Josh Rodgers Jan 30 '13 at 22:13
To make sure my understanding is correct. You want to "do something" on all children of a specific Parent Page. Right? If so then this should work if(is_page() && $ancestor == $pid) { "do this"; }else{ echo "do this instead"; } That should fire off if you're on a page and that page is an ancestor of $pid where it's the page id – Alex V. Jan 30 '13 at 22:25
Alex, I have a main page called "People" then I have subpages or child pages called "Oscar", "Billy", and "Tim"...right now my code displays on all three subpages "Oscar", "Billy", and "Tim". I would like it to display on "Billy" only, if that's possible :) sorry for the confusion. – Josh Rodgers Jan 30 '13 at 23:58
This code would only run on a page with a post named 'Billy' 'if (is_page('Billy')) { echo "do this"; }else{ echo "do this instead"; }' – Alex V. Jan 31 '13 at 0:30
Alex, I don't have a post named "billy", I have a child page named "billy" :-( – Josh Rodgers Feb 1 '13 at 20:52
up vote 0 down vote accepted

I don't know what I was thinking earlier...I guess somehow I was thinking that is_page wouldn't work for my child page, but I was over-thinking...this is my final code:

<?php if (is_page("duck")) { 
    echo "do something";
} else { 
    echo "something else";
} ?>

I know, I know it's so simple, but it works - guess I was just having a brain fart.


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.