Support » Fixing WordPress » how to find ID of main page when on a sub-sub-page?

  • lxg


    I’m building a WordPress-based website where I have a horizontal main navigation bar, and a sidebar listing all pages starting from second level for the respective top level page. While the main bar is no problem, I need to find a way to display only the second level pages and below of the current “main page”.

    There is a parameter child_of for wp_list_pages(), and there is the $post class with for ID and post_parent properties. But in order to get it work on third level pages, there would be the need to find out the ID of the top level page.

    How can I find out the ID of a any page’s root page? Thanks for any ideas.

    (btw, a similar problem has been discussed here, but with no solution.)

Viewing 2 replies - 1 through 2 (of 2 total)
  • Anyone found a way to accomplish this? I would really need this type of function, but I am not sure how to backtrack each page parent!

    Ok, I made a hybrid of another, lookalike script:

    function get_root($id) {
    	global $post, $wp_query, $wpdb;
    	$parent = 1;
    	while($parent) {
    		$page_query = $wpdb->get_row("SELECT ID, post_parent FROM $wpdb->posts WHERE ID = '$id'");
    		$parent = $id = $page_query->post_parent;
     		   $last = $page_query->post_parent;
    	return $last;
    $current_page = $wp_query->get_queried_object_id();
    $root_page = get_root($current_page);
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to find ID of main page when on a sub-sub-page?’ is closed to new replies.