I have a section with an text field and an image field. the asset field may sometimes be left blank. in this case, in my template, i want to grab the image from another random entry.
This is the code I'm trying to use, but when the image field is blank in my initial entry, I'm getting an error that Variable "image" doesn't exist. Am I calling the random image incorrectly?
{% for stat in craft.entries.section('statistics').limit(1).order('RAND()') %}
{% if stat.image|length %}
{% set image = stat.image.first() %}
{% else %}
{% for random in craft.entries.section('statistics').limit(1).image(':notempty:').order('RAND()') %}
{% set image = random.image.first() %}
{% endfor %}
{% endif %}
{{ image.url('medium') }}')
{% endfor %}
{% set stat = craft.entries.section('statistics'.limit(1).order('RAND()')).image(':notempty') %}