1

I'm trying to make it so that if an entry image does not exist, then fall back to the category image that the entry belongs to. Right now this isn't working and is receiving an error.

{% set feature = entry.brandFeature.one() %}
{% set featureUrl = feature ? feature.url : entry.category.one().categoryImage.getUrl() %}
<div class="jumbotron category-jumbotron" style="background-image:url({{ featureUrl }})">

This is the error that craft is returning:

Calling unknown method: craft\elements\db\AssetQuery::getUrl()

2
entry.brandFeature.one()

Is correct, you'll access the asset field and execute the one() function in order to fetch the asset.

entry.category.one()

Is correct too. You'll fetch the first category of your categories field

entry.category.one().categoryImage

Will access the categoryImage asset field of your category.

But then you do

categoryImage.getUrl()

So you try to get the url from the field value but you don't actually grab the asset that might be stored in the category.

categoryImage.one().getUrl()

However - keep in mind there could be entries without a category. Then you'll receive an error as well.

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.