pers0n Posted April 24, 2014 Share Posted April 24, 2014 I'm not able to get my array list. I have a menu template and I'm grabbing it, but it won't let me go inside it I get this error Warning: Invalid argument supplied for foreach() in /x/site/templates/includes/header.inc on line 61 $items = $pages->find('template=menu, title=Toolbar Menu, include=all'); //THIS SHOULD WORK foreach ($items->menu_item as $item) { $output .= '<li><a href="' . $item->link . '"'; if ($item->open_in_new_window) $output .= ' target="_blank"'; $output .= '>' . $item->title . '</a></li>'; } I'm attaching a screenshot of a selector test Link to comment Share on other sites More sharing options...
diogo Posted April 24, 2014 Share Posted April 24, 2014 You can foreach $items but not $items->menu_item. What is the menu_item field by the way? A page field? 1 Link to comment Share on other sites More sharing options...
teppo Posted April 25, 2014 Share Posted April 25, 2014 Based on the screenshot menu_item is definitely a page field. $pages->find() gives you an instance of PageArray and like @diogo already mentioned, you can't ask field "menu_item" of that PageArray -- it contains multiple Page items and doesn't have any fields of it's own. You'll have to first get individual Pages out of that PageArray: // this gives you first Page object found with your selector; if you're sure // that there's never going to be more than one Page, this should be fine $items = $pages->find('template=menu, title=Toolbar Menu, include=all'); foreach ($items->first()->menu_item as $item) { ... } // another thing you can do to get only the first result is use 'get' instead of 'find' $items = $pages->get('template=menu, title=Toolbar Menu, include=all'); foreach ($items->menu_item as $item) { ... } // if there's a possibility of multiple 'menu' pages, you need to use another foreach $items = $pages->find('template=menu, title=Toolbar Menu, include=all'); foreach ($items as $menu_page) { foreach ($menu_page->menu_item as $item) { ... } } Got it? 2 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