• Resolved grosbouff

    (@grosbouff)


    Hi, how can I get the FIRST parent of a page ?

    page–>subpage–>subsubpage

    I mean, when I’m in the subsubpage and that I check
    $post->post_parent; it gives me the ID of the subpage and not of the page ?

    Avoiding a loop could be great too…

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Avoiding a loop could be great too…

    Sorry, it can’t be done without looping or making a recursive call (which is the same basic thing).

    Something like this would do it:

    function get_root_parent($page_id) {
    	$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
    	if ($parent == 0) return $page_id;
    	else return get_root_parent($parent);
    }

    Basically, you give it a page id and get back the root parent. But it has to navigate its way up through the pages to find the root.

    Thread Starter grosbouff

    (@grosbouff)

    Okay, thanks !
    But this isn’t working.
    Maybe a typo somewhere ?

    function get_root_parent($page_id) {
    				$query = 'SELECT post_parent FROM $wpdb->posts WHERE post_type="page" AND ID = "'.$page_id.'"';
    				$parent = $wpdb->get_var($query);
    				if ($parent == 0) return $page_id;
    				else return get_root_parent($parent);
    			}
    
    $parent_page = $post->post_parent;
    get_root_parent($parent_page);
    
    Fatal error: Call to a member function get_var() on a non-object in /menu.php on line 11
    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    This is WordPress code. It has to be run as part of WordPress. What is “menu.php”? That’s not part of WordPress, is it?

    Thread Starter grosbouff

    (@grosbouff)

    no, it is one of my templates included

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Try adding global $wpdb; as the first line in the function.

    Thread Starter grosbouff

    (@grosbouff)

    function get_root_parent($page_id) {
    global $wpdb;
    	$parent = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE post_type='page' AND ID = '$page_id'");
    	if ($parent == 0) return $page_id;
    	else return get_root_parent($parent);
    }

    Ok, this one works.
    Thanks, Otto42 !

    hmm, sounds good.
    how do you use this to highlight the topmost parent in a menu structure?
    is there a code you add to the wp_list_pages call in the header to keep the “grandparent” highlighted?

    Thread Starter grosbouff

    (@grosbouff)

    this is a piece of my menu where it "happens" :)
    
    		$onglets = wp_list_pages('title_li=&depth=1&echo=0');
    
    		$onglets = explode("<a href=\"", $onglets);
    
    		for($i=1;$i<sizeof($onglets);$i++) {
    
    			$ongletclass = "";
    
    			$onglet_url = explode("\">", $onglets[$i]);
    
    			$onglet_url2 = explode("\" title=\"", $onglet_url[0]);
    
    			$onglet_title = $onglet_url2[1];
    
    			$onglet_vars = explode("?", $onglet_url2[0]);
    
    			$onglet_vars = "&".$onglet_vars[1];
    
    			parse_str($onglet_vars, $output);
    
    			//ID PAGE ONGLET
    
    			$onglet_id = $output['page_id'];  // value
    
    			if ( (is_page()) && (empty($onglet_id)) ) { //bug de la HOME qui n'affiche pas de var page_id;
    
    				$onglet_id = $post->ID;
    
    			}
    
    			if (($root_page) == $onglet_id){
    
    				$ongletclass ='class="default"';
    
    			}
    
    			echo'<dl '.$ongletclass.'><dt id="onglet'.$i.'">';

    okay, looks like you use the url to see how deep you are.
    thanks for the code … but i don’t think i can use it.
    but someone else might ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘get FIRST parent of a page’ is closed to new replies.