How to Check if a Page is a Child using Advanced ID or Slug in WordPress

Home » Snippets » How to Check if a Page is a Child using Advanced ID or Slug in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a way to determine if a page is a child page in WordPress? Being able to identify whether a page is a child or a parent can be helpful for implementing conditional functionality or styling on your website. In this article, we will show you how to check if a page is a child using its advanced ID or slug. By the end, you’ll have the knowledge to effortlessly determine the hierarchical relationship between pages in your WordPress site.

					function wpturbo_is_page_child_advanced($parent, $child){
    global $post;
    if(is_int($parent)) {
        $parent = get_page_by_path(get_page_uri($parent));
    } else {
        $parent = get_page_by_path($parent);
    }
    if($parent){
        $parent_id = $parent->ID;
    }

    if(is_int($child)) {
        $child = get_page_by_path(get_page_uri($child));
    } else {
        $child = get_page_by_path($child);
    }

    if($child){
        $child_id = $child->ID;
    }else{
        return false;
    }

    if($post->post_parent == $parent_id || ($post->post_parent == $child_id && $post->ID == $child_id)){
        return true;
    }
    return false;
}
				

The code snippet provided defines a function called wpturbo_is_page_child_advanced that is used to check if a page is a child of another page in WordPress. The function takes two arguments: $parent and $child, which can either be the ID or the slug of the parent and child pages.

To begin, we start by retrieving the global $post object, which represents the current page. We then proceed to check if the $parent argument is passed as an integer or a string. If it is provided as an integer, we use the get_page_by_path function to retrieve the parent page using its URI slug. On the other hand, if the $parent argument is provided as a string, we directly retrieve the parent page using the get_page_by_path function.

Next, we check if the $parent variable has a value (indicating that the parent page exists). If it does, we assign the ID of the parent page to a variable called $parent_id. This helps us conveniently compare the current page’s parent with the specified parent page.

Similarly, we perform the same steps for the $child argument. If the $child argument is provided as an integer, we use get_page_by_path to retrieve the child page using its URI slug. If it is provided as a string, we directly retrieve the child page using the get_page_by_path function. We assign the ID of the child page to a variable called $child_id.

If the $child variable has a value (indicating that the child page exists), we proceed to compare the post parent of the current page ($post->post_parent) with the $parent_id and $child_id variables. If the post parent matches either the parent ID or the child ID, we can conclude that the current page is a child of the specified parent page. In addition, we also check if the child ID matches the current page ID, ensuring that the function doesn’t mistakenly detect the child page as its own parent.

If any of these conditions are met, we return true indicating that the current page is a child of the parent page. Otherwise, we return false.

This function can be useful in various scenarios, such as when you want to conditionally display certain content or perform specific actions based on whether a page is a child of another page in your WordPress website.

Register an account to save your snippets or go Pro to get more features.