David Beesley Posted July 26, 2016 Share Posted July 26, 2016 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 More sharing options...
ottogal Posted July 26, 2016 Share Posted July 26, 2016 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.) 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted July 26, 2016 Share Posted July 26, 2016 No need for the if statement here: $hero_images = $page->closest("hero_gallery.count>0")->hero_gallery; (Also the count selector is incorrect above) 3 Link to comment Share on other sites More sharing options...
David Beesley Posted July 26, 2016 Author Share Posted July 26, 2016 Perfect LK. Thanks very much Link to comment Share on other sites More sharing options...
ottogal Posted July 26, 2016 Share Posted July 26, 2016 @LostKobrakai Quote the count selector is incorrect above It should be like this, shouldn't it? $page->parentsUntil("hero_gallery.count>0") If not, could you explain it a bit further? Link to comment Share on other sites More sharing options...
LostKobrakai Posted July 26, 2016 Share Posted July 26, 2016 Yeah, the selector should be like mine in both our examples. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now