How to return a Boolean for a page that is a subpage thru its name?

I am attempting to check for a page that is a subpage using its name. For example

https://www.myexample.com/mypage/subpage_anything_1/andso_on

Here www.myexample.com is the web domain, mypage is the page and subpage_anything_1 is the subpage. We can check if we are on a particular page using is_page() like

if(is_page(‘mypage’)){//mycode}

When I do the same for a page name that is a subpage like

if(is_page(‘subpage_anything_1’)){//mycode}

The condition does not work and from my guess, its returning as false even when I am on that subpage.

There is a related discussion in stockoverflow that uses this code

<?php

global $post; // if outside the loop

if ( is_page() && $post->post_parent ) {
// This is a subpage

} else {
// This is not a subpage
}
?>

but I think it would return true for any subpage, what I am attempting instead a is Boolean check for a specific subpage only via its name.

What is the best solution to this?

I tried:

is_page(‘mypage/subpage_anything_1’) //does not work
is_child_page(‘subpage_anything_1’) //I did not see any documentation

Read more here:: How to return a Boolean for a page that is a subpage thru its name?

Leave a Reply

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