Jump to content

Tree Traversal for Content


David Beesley
 Share

Recommended Posts

Hi All,

First time poster, so please go easy on me.

I am trying to write a small block of code so that if that if the page I am on doesn't have a "Hero Image" it will traverse up the tree until it finds a page that does have one.

I am attempting to utilise $page->parentsUntil() to do so. Am I on the right track?

if (!empty($page->hero_gallery)) {
	$hero_images = $page->hero_gallery;
} else {
	$hero_images = $page->parentsUntil("hero_gallery!=''")->hero_gallery;
}

This code is currently returning nothing :(

Link to comment
Share on other sites

Welcome to the community!

Have a read of  this thread. So you should try something like this:

if (count($page->hero_gallery)>0) {
	$hero_images = $page->hero_gallery;
} else {
	$hero_images = $page->parentsUntil("count(hero_gallery)>0")->hero_gallery;
}

(Just written in the browser, didn't try it myself.)

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...