Jump to content

can't access image field


Ovi
 Share

Recommended Posts

This is driving me nuts. It's such a simple thing, I've done it 100s of times before, but right now it's not working. It's either:

  • an obvious mistake on my part
  • Processwire broke (hope not)

I'm trying to do the simplest thing: list the images in an image field, like this:

<?php if (count($page->homepage_slideshow)): ?>
  <ul class="campHpSlideshow">
  <?php foreach ($page->homepage_slideshow as $slide): ?>
    <li><img src="<?= $slide->url ?>" alt="<?= $slide->description?>"></li>
  <?php endforeach ?>
  </ul>
  <!-- /.homepageSlideshow -->
<?php else: ?>
  <img src="http://placehold.it/1024x400&text=no+images" alt="">
<?php endif ?>

The count($page->homepage_slideshow) returns false, even though there are images in the field. The field name is spelled correctly. I triple-checked.

Also, I could swear this was working before. Any ideas what I can do to fix this? Would re-installing help? 

  • Like 1
Link to comment
Share on other sites

Silly question:

Do you have this code on multiple pages running, say on the basic-page template? And you have populated all those pages ?

The FALSE returning is a little bit weird, as it should be an integer. ( Sometimes I use the homepage for storing slideshow images.

And I have a variable name for the home named $home. Then I pull it from $home->homepage_slideshow. )

Or maybe you're just putting it in the wrong template or editing a different install ( been there, done that ). BTW, you code looks fine.

  • Like 2
Link to comment
Share on other sites

OMG. Found the error! This needs to be a cautionary tale to others.

I had this code in the head section: 

<?php if ($page->template = "typical-day"): ?>
	<!-- some files get added here-->	
<?php endif ?>

As you can see there is a "=" sorely missing there (i miss-typed or was just tired, have no clue how it happened).

So it ended up changing the template of the page instead of just checking if it was set to "typical day". The "typical-day" template obviously did not even have the "homepage_slideshow" field assigned to it.

On the bright side

That is something very cool to be able to do (changing the template on run-time), which might have some interesting applications. Is that something one should do or should we stay away from it?

  • Like 2
Link to comment
Share on other sites

This is one of those things that gets everyone every now and then. One way to to avoid it is to define them in opposite order, so if you accidentally forget the second = you will get an error, instead of setting the variable to the value

Like this:

if ("typical-day" = $page->template)

will throw a parse error, but with "==" it will work as expected.

As for whether you should change templates at run-time - there should be no problems doing this and it can be quite powerful.

  • Like 4
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...