0

My need is to check if a page is subpage of a page (with ID).

I try to get this code working but it doesn't.

function is_child($pageID) {
  global $post;
  echo $post->post_parent; // display the right ID!
  if( is_page() && $post->post_parent == $pageID ) {
    return true;
  } else {
    return false;
  }
}

It returns always false while $post->post_parent returns the right ID!

Testing code (which always returns no while $post->$post_parent echoes the good ID in the function):

if(is_child(2310)) {
    echo 'yes';
} else {
    echo 'no';
}

This function takes place in my functions.php file and its purpose is to load CSS through a condition statement (load a particular CSS file if page is X or child of X).

This code can be found on many sites around there but was produced in 2012-2013.

Thanks a lot for any help.

Sources of unworking codes : https://bavotasan.com/2011/is_child-conditional-function-for-wordpress/ https://www.kevinleary.net/wordpress-is_child-for-advanced-navigation/

10
  • 2
    You’re using the global $post variable, which means the result will depend highly on where exactly it’s used. Where is the function being used? – Jacob Peattie Nov 14 '17 at 14:50
  • I've written it in my functions.php. So it's called on every page and for my testing purpose I check it on a child page with the ID of its parent. With a simple echo in an if statement. The function is well played and I can echo the parent's ID in it. – Valentin Nov 14 '17 at 15:01
  • Please make a note your is_page function has no parameter in it. developer.wordpress.org/reference/functions/is_page – prosti Nov 14 '17 at 15:09
  • @prosti parameter for is_page is optional. Even without it, this code doesn't work. – Valentin Nov 14 '17 at 15:34
  • Are you sure you are working with pages? Maybe you used posts developer.wordpress.org/reference/functions/is_single – prosti Nov 14 '17 at 15:47
4
/**
 * Return whether the current page is a child of $id
 *
 * Note: this function must be run after the `wp` hook.
 * Otherwise, the WP_Post object is not set up, and
 * is_page() will return false.
 *
 * @param  int  $id The post ID of the parent page
 * @return bool     Whether the current page is a child page of $id
 */
function is_child_of( $id ) {
  return ( is_page() && $id === get_post()->post_parent );
}
5
  • 1
    I finally get it work but found your answer very nice. Thanks a lot @NathanJohnson – Valentin Nov 14 '17 at 16:22
  • This looks nice but I wonder why the original code had the problem ? – prosti Nov 15 '17 at 14:07
  • 1
    Good question. I saw the question, wrote and tested my answer without testing the original code. I've now tested the original code, my code, and the answer by the OP which they state works. All 3 are functionally identical. I cannot come up with a case where they return different results. So my answer answers the question posed in the OP, but doesn't answer why the OPs original code didn't work. My guess, though I certainly don't know for sure, is that the OP was trying to call the function too soon so that is_page() was returning false. – Nathan Johnson Nov 15 '17 at 17:47
  • This is a great effort @NathanJohnson, it deserves a respect. – prosti Nov 15 '17 at 23:53
  • Yes @NathanJohnson you might be right. And obviously global $post was not needed at all. Thanks again for your elegant answer. – Valentin Nov 16 '17 at 8:32
1

So I did this and it works. But @NathanJohnson answer is nicer.

function is_child($pageID) {
  $parentID = wp_get_post_parent_id(get_the_ID());
  if( is_page() && $parentID == $pageID ) {
    return true;
  } else {
    return false;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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