WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Jan 02 2012

How to Determine Child/Ancestor with is_child() and is_ancestor()

I found this great piece of code at Codebyte.
Put the following in your functions.php file:
[php]
// Check if page is direct child
function wps_is_child( $page_id ) {
global $post;
if( is_page() && ( $post->post_parent == $page_id ) ) {
return true;
} else {
return false;
}
}
[/php]
[php]
// Check if page is an ancestor
function wps_is_ancestor( $post_id ) {
global $wp_query;
$ancestors = $wp_query->post->ancestors;
if ( in_array( $post_id, $ancestors ) ) {
return true;
} else {
return false;
}
}
[/php]

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Pippin says

    January 2, 2012 at 10:42 am

    Great functions (I’ve used them several times), but please remember to prefix the function names with some unique prefix. This helps prevent function conflicts.

    Reply
    • Travis Smith says

      January 2, 2012 at 11:31 am

      Thanks Pippin. Updated the post as we always should use namespaces!

      Reply
      • Pippin says

        January 2, 2012 at 11:35 am

        Nice, much better 🙂

        Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in