First()->url() causes error

I am trying to get the URL of the first image on a page.
this will display the image just fine, but when I add
it causes it to be an error.

Yet further down in the template,
<a href="<?= $article->url() ?>" class="article-more">read more</a>
this url() works fine

The reason I want the img url is because I want to be able to add the align=“left” property to the img tag, i.e.
<img src="<?= $article->images()->first()->url()?>" align="left" />

What sort of error do you get? Probably “Call to a member function url() on null”, right? That means that the image object does not exist. Do all your articles have images? Also, check out this cookbook recipe.


No, not all articles have images, that’ll be it. Thanks a lot. And that cookbook recipe is exactly what I needed. Thanks again @texnixe ! :grinning: