WordPress.org

Support

Support » Themes and Templates » Get page depth

Get page depth

  • Joe Hoyle

    @joehoyle

    Human Made

    Hi,

    Is it possible to use a template tag to get the current page depth as I want to style sub pages slightly differently to the parent pages. If I could get the current page depth I could create a simple If statement to run whatever code when the page has a depth of > 1.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • I need to do this too. Anyone know how?

    Here is some code you can use:

    <?php
    
    global $wp_query;
    $object = $wp_query->get_queried_object();
    $parent_id  = $object->post_parent;
    $depth = 0;
    while ($parent_id > 0) {
    	$page = get_page($parent_id);
    	$parent_id = $page->post_parent;
    	$depth++;
    }
    
    echo $depth;
    
    ?>

    If you add that in the loop in your page.php file, it will display the depth of the page. You could use the $depth value in a conditional statement instead of course.

    Main pages will show a depth of 0, sub pages 1, 2, etc..

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get page depth’ is closed to new replies.