How To Check That Current Page Is Subpage Or Having Parent Page In WordPress

Hi Friends, When I was working on one wordpress project, I got one problem in which I have to find that current page is subpage or not or in other terms current page having any parent page or not? WordPress dont have any function for this, So I have tried to make a function for it by which we can check that current page is subpage or not also if it is subpage then it will return parent page id and if it is not a subpage then it will return false.

Below are function by which we can check that current page is subpage or having parent page.

function check_is_subpage() {
	global $post;                                 // load details about this page
        if ( is_page() && $post->post_parent ) {      // test to see if the page has a parent
               return $post->post_parent;             // return the ID of the parent post
        } else {                                      // there is no parent so...
               return false;                          // ...the answer to the question is false
        }
}

Now if you want to use this function then open your functions.php file of your theme and then put it anywhere in functions.php. After that you can use it like this :

$result = check_is_subpage(); // if page will be subpage then $result will have parent page id else function will return false in $result.

So this is function by which we can check that current page is subpage or not. Let me know if you have any problem in this I will try to solve your problems.

I am WordPress Developer India , PHP Freelancer , So If you have any projects related related to WordPress, PHP or anything related to PHP, You can contact me.

Subscribe to PHP Freelancer

Enter your email address: