WordPress.org

Ready to get started?Download WordPress

Forums

how to find ID of main page when on a sub-sub-page? (3 posts)

  1. lxg
    Member
    Posted 8 years ago #

    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.)

  2. Joseelsantos
    Member
    Posted 7 years ago #

    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!

  3. Joseelsantos
    Member
    Posted 7 years ago #

    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;
    		if($parent)
     		   $last = $page_query->post_parent;
    	}
    	return $last;
    }
    $current_page = $wp_query->get_queried_object_id();
    $root_page = get_root($current_page);

Topic Closed

This topic has been closed to new replies.

About this Topic