How to write a Function - is_page('15') and/or child of '15'

I want to write a function that states 'Do X' if is_page('15') or is child of '15'. How can I write that?

Thanks,
Chris

  • Sajid

    Hi @admin941,
    Hope you are doing good today :slight_smile:

    To check if its a page you can use is_page() conditional tag, further you can check for specific page by adding the page_id or page slug. You can find more details about this conditional tag here:
    https://developer.wordpress.org/reference/functions/is_page/

    To check if current page is parent of a page, you can either pass the slug or parent page in the function. For example A is Parent and B is child then you can check if current page B is child of A using following code, where A and B are slugs of pages respectively.

    if( is_page('A/B') ){
    //Current page is B and child of A.
    }

    Further more you can also check using following custom conditional tag where current page is parent of a specific page. 283 is the ID of the parent page.

    if ( 283 == $post->post_parent ) {
        echo 'this post has a parent of 283';
    }

    Hope that helps! Feel free to post a reply if you need further assistance :slight_smile:
    Cheers, Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.