Jump to content

Not able to get pagearray values


pers0n
 Share

Recommended Posts

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

post-161-0-55379800-1398376999_thumb.png

Link to comment
Share on other sites

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?

  • 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

×
×
  • Create New...