Jump to content

Pages->get() works on every page except the page with the field


Barry
 Share

Recommended Posts

$footer_items = $pages->get("/")->nav_footer;
foreach($footer_items as $footer_item){
	echo "<li><a class='menu-item' href='$footer_item->url'>$footer_item->title</a></li> ";
}

I have this code in my footer which is an included php file. On my homepage template I have a page field with multiple pages selected which I use to create a row of links. This works on every single page/template except on the homepage itself where I get a Warning: Invalid argument supplied for foreach().

I've also tried using

$footer_items = $page->nav_footer;

 That also gives me an error. What am I missing?

Link to comment
Share on other sites

Some debugging ideas:

// check which fields are added to the page
dump($page->fields->explode('name'));
dump($pages(1)->fields->explode('name'));

// get the first page reference field of the page
$pageFieldName = $page->fields->findOne('type=FieldtypePage')->name;
dump($page->$pageFieldName);

 

Link to comment
Share on other sites

48 minutes ago, kongondo said:

Strange one. I am wondering if some code is overriding $page when viewing the home page. Do other fields on 'Home' work fine?

I've tested this and $page->name and title and url all work fine. My homepage template doesn't have any other custom fields besides the already mentioned footer_nav

Link to comment
Share on other sites

8 minutes ago, Barry said:

if($page->template = 'pageGeo')

One trick is to do this instead...

if('pageGeo' == $page->template)

If you mistyped that as:

if('pageGeo' = $page->template)// syntax error
// OR even
if(1 = $page->id)// syntax error

PHP will throw a Parse Error: syntax error, unexpected '=' , immediately alerting you of your mistake :).

  • Like 4
Link to comment
Share on other sites

8 hours ago, kongondo said:

If you mistyped that as:


if('pageGeo' = $page->template)// syntax error
// OR even
if(1 = $page->id)// syntax error

I have never thought of it but it is so obvious. We are simply trained to do it the other way round. 

Anyway, my NetBeans marks such an issue on the fly which is hard to miss, and that is why I do not make this mistake these days, otherwise... :)

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...