FORUMS CLOSED DUE TO SPAM. YOU STILL CAN ADD POST!

Trying to get root ancestor page on level 1

Feed 3 posts, 2 voices

Avatar
35 posts

if i have the following tree

home
news
 `article1
    `subpage
 `article2

i want to know the ancestor page from any subpage. So if i am on “subpage” i want to know that the ancestor (on level 1) is “news” page.

here is my code, which seems to work except the recursion does not recur…. any ideas what is wrong?

function getRootPageTitle( $page ){	
	if( $page->level() <= '1' ){ 
		return $page->title;
	}else {
		getRootPageTitle( $page->parent() );
	}
}
echo getRootPageTitle( $this );
 
Avatar
35 posts

Ah forgot to return a value from the recurred function:

function getRootPageTitle( $page ){	
	if( $page->level() <= '1' ){ 
		return $page->title;
	}else {
		return getRootPageTitle( $page->parent() );
	}
}
echo getRootPageTitle( $this );
 
Avatar
1493 posts

This is quite nifty, avagraphique! Thanks!

Can see uses for this…

 
Avatar
1493 posts

And just now connecting this up from something similar by Andy (for slug).