1

I have the 404 page setup as a single in sections under page404.

I have a postImage field that pulls images from assets. It is assigned to the page404 field layout and it is assigned in the entry.

I tried the this template approach:

{% set notFound = craft.entries.section('page404') %}
<img src="{{ notFound.postImage.first().url }}" width="800" height="800" alt="">

but I keep getting the following error:

Impossible to access an attribute ("url") on a null variable

What am I doing wrong, and how should it be template?

1

You left off the .first().

{% set notFound = craft.entries.section('page404').first() %}

Without that, the Element Criteria Model will default to .find().

  • first - Returns a single Entry Model.
  • find - Returns an array of Entry Models.

It doesn't matter this this is a Single section! All Element Criteria Models have the same behavior, whether they're fetching data from a Single, Channel, or Structure.

4
  • Three items... 1) I no longer get an error. And I can print the body field using {{ notFound.body }} ...but... 2) The image using {{ notFound.postImage.first().url}} does not display, even though it is assigned. ...and 3) why is it possible to use {{entry.title}} or {{ entry.body}}` without defining entry -- and here I am using the crafty brewery or default news channel in Craft.
    – Hellyar
    Sep 14 '16 at 20:53
  • Do you have devMode enabled? I feel like some of these errors would be more apparent with that on.
    – Lindsey D
    Sep 14 '16 at 21:02
  • Yes, and there is no error being thrown.
    – Hellyar
    Sep 14 '16 at 23:05
  • Strange. The thumb of the image appears in assets, with the correct file size. It appears in the entry. But it was not visible in assets when I tried to "view asset". I was also unable to "replace file". It said the file was "empty." Anyway, I assume that is just a quirk. I deleted the asset and uploaded another copy. Everything works now. Many thanks.
    – Hellyar
    Sep 14 '16 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.