shortcode – Check if parent page has child page of certain slug


I currently have a function that checks to see if the page is a parent, and if so, do stuff. But I also want to check to see if the parent has a child page that has a certain slug.

Basically, how would I check to see if a WordPress page has a child that matches a slug of my-child-slug?

In short, this is my current function:

/* Using Slug, make iconbox Shortcode */
function fs_sc_location_iconbox( $atts ){ 
    // begin output buffering

    global $post; // if outside the loop

    $response="Nothing to see here";

    if ( $post->post_parent ) {
        // This is a subpage
        $slug = $post->post_name;

    } else {
        // This is not a subpage
        $slug = $post->post_name;

        // Check if has child page with slug of my-child-slug
        // ** NEED LOGIC HERE **

        // if this parent page has a child with the desired child slug...
        $response="Yes, this parent has a child page that we are looking for.";


    // if there is a slug
    if ($slug) {
        // output the default response, unless this page is a parent of the desired child page
        echo $response;
    // end output buffering, grab the buffer contents, and empty the buffer
    return ob_get_clean();
add_shortcode( 'fs_location_iconbox', 'fs_sc_location_iconbox' );

How can I updated te // ** NEED LOGIC HERE ** area, so that it checks if this parent page has a child of the my-child-slug slug? And if so, then it will set a new value for the $response variable.

Garconis 2 years 2021-04-22T12:09:41-05:00 0 Answers 0 views 0

Leave an answer