Barry

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

Recommended Posts

Barry    18
$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?

Share this post


Link to post
Share on other sites
kongondo    5,014

Works fine here. What does debug and/or Tracy tell you is the value of $footer_items when viewing the home page? Maybe there's some template level restriction?

Share this post


Link to post
Share on other sites
Barry    18
8 minutes ago, kongondo said:

Works fine here. What does debug and/or Tracy tell you is the value of $footer_items when viewing the home page? Maybe there's some template level restriction?

NULL

Share this post


Link to post
Share on other sites
abdus    596

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);

 

Share this post


Link to post
Share on other sites
kongondo    5,014
1 hour ago, Barry said:

NULL

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

Share this post


Link to post
Share on other sites
Barry    18
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

Share this post


Link to post
Share on other sites
kongondo    5,014

Only thing left to look at is your relevant template files code, the code in the include file as well as the settings of your page field (just in case). Any restrictions on who can view home page?

Share this post


Link to post
Share on other sites
Barry    18

embarrassed to admit that after some thorough digging, I found 

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

instead of with == which was messing up everything.

 

  • Like 2

Share this post


Link to post
Share on other sites
adrian    7,664
6 minutes ago, Barry said:

embarrassed to admit that after some thorough digging, I found 


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

instead of with == which was messing up everything.

 

You're in good company - we've all done that one :)

  • Like 1

Share this post


Link to post
Share on other sites
kongondo    5,014
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

Share this post


Link to post
Share on other sites
szabesz    1,378
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... :)

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.