In this tutorial we are going to show you, how to use conditional tags in wordpress. We given code sample for wordpress theme. How to check page is subpage or parent page.
We got this particular requirement of wordpress menus. For that we need many times parent page menu and sub pages as submenu.
how to use conditional tags in wordpress
Using following code we can able to find the parent page and sub pages from wordpress themes. Following code we can put any where in page or code. We can put this code in header.php or index.php or footer.php or anywhere, as per our requirement.
<?php global $post; // if outside the loop if ( is_page() && $post->post_parent ) { // This is a subpage } else { // This is not a subpage } ?>
If you want use or add the is_subpage() method then you need to add the following code in functions.php file.
<?php function is_subpage() { global $post; // load details about this page if ( is_page() && $post->post_parent ) { // test to see if the page has a parent $parentID = $post->post_parent; // the ID of the parent is this return $parentID; // return the ID } else { // there is no parent so... return false; // ...the answer to the question is false }; }; ?>
Nice information for using the conditional tag for wordpress developers.