WordPress Snippets at WPcustoms

Create conditional tag for parent and child pages

If you want to use custom code via conditional tags for parent pages and its child pages there is no conditional tag built in.
This function is a workaround and can be used like every other conditional tag.


/**
 * Snippet Name: Create conditional tag for parent and child pages
 * Snippet URL: https://wpcustoms.net/snippets/create-conditional-tag-parent-child-pages/
 */
  // usage: if (is_parent_and_child(13)) {   // we're at the page or at a sub page   }

function is_parent_and_child($pid) {      // $pid = The ID of the page we're looking for pages underneath
	global $post;         // load details about this page
	if(is_page()&&($post->post_parent==$pid||is_page($pid))) 
               return true;   // we're at the page or at a sub page
	else 
               return false;  // we're elsewhere
};