Skip to content

Instantly share code, notes, and snippets.

@knolaust
Last active January 30, 2024 15:07
Show Gist options
  • Save knolaust/2cb49a988421f862554d59aea13e2e46 to your computer and use it in GitHub Desktop.
Save knolaust/2cb49a988421f862554d59aea13e2e46 to your computer and use it in GitHub Desktop.
This function (add to functions.php file) creates new logical function to determine if page is parent or child
<?php
/**
* Check if Page is a Child of Another Page
* Gist Keywords: wordpress, parent, child, hierarchy
*
* @category WordPress
* @author Knol Aust
* @version 1.0.0
* @description Function to determine if the current page is a child of a specific parent page.
*/
// Include this function in functions.php
function knolaust_is_tree($pid) {
global $post;
if (is_page() && ($post->post_parent == $pid || is_page($pid))) {
return true; // The current page is the specified parent or a subpage of it
} else {
return false; // The current page is not related to the specified parent
}
}
// Include this code in your page template, sidebar, etc.
if (knolaust_is_tree(2)) {
// Perform actions for pages that are children of page with ID 2
// You can replace 2 with the ID of the desired parent page
// Do amazing stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment