How to Get the Depth of the Current Page in WordPress

Home » Snippets » How to Get the Depth of the Current Page 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 a WordPress developer looking to retrieve the depth of the current page? Knowing the depth of a page can be useful for various purposes, such as creating custom navigation menus or styling elements based on the page hierarchy. In this article, we will show you how to easily get the depth of the current page in WordPress using a simple code snippet. Let’s dive in and explore this handy technique!

					function wpturbo_get_depth($parent_id = 0, $depth = 0) {
    global $post;

    if ($parent_id == 0) {
        $page_id = $post->ID;
    } else {
        $page_id = $parent_id;
    }

    $ancestors = get_ancestors($page_id, 'page');

    if ($ancestors) {
        $depth = count($ancestors) + 1;
    }

    return $depth;
}
				

The code snippet above defines a function called wpturbo_get_depth(), which is used to calculate the depth of the current page in WordPress. The depth refers to the number of parent pages a page has.

To begin, we set the default values for the function parameters $parent_id and $depth. The default value for $parent_id is 0, which means we are initially assuming that the current page has no parent. And the default value for $depth is also 0, since we don’t have any parent pages yet.

Next, we use the global $post variable to access the global $post object, which contains information about the current page being displayed. We need this object to obtain the ID of the current page.

We then check if the $parent_id variable is 0. If it is, it means that we are calculating the depth for the current page and not for any of its child pages. In this case, we assign the ID of the current page to the variable $page_id.

However, if the $parent_id variable is not 0, it means that we are calculating the depth for one of the child pages. In this case, we assign the value of $parent_id to the $page_id variable.

After obtaining the ID of the page, we use the get_ancestors() function to retrieve an array of all the parent page IDs for the given $page_id. We specify 'page' as the second argument to indicate that we are only interested in the parent pages that are of the page post type.

If the $ancestors array is not empty, it means that the current page has parent pages. In this case, we update the value of $depth by adding 1 to the count of the $ancestors array. The count function returns the number of elements in the array, which represents the number of parent pages.

Finally, we return the value of $depth, which now represents the depth of the current page.

To use this function, you can simply call it wherever you need to obtain the depth of the current page and store the returned value in a variable. For example:

$current_page_depth = wpturbo_get_depth();
echo "The depth of the current page is: " . $current_page_depth;

This will output the depth of the current page on the front end of your WordPress site. You can then use this value to perform any specific actions or display relevant information based on the depth of the current page.

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