<p>Welcome to the Craft community @bjyama</p>

<pre><code>{# Step one: pick a random service #}
{% set randomService = craft.entries.section('services').relatedTo(left).order('RAND()').first() %}

{# Step two: pick a random image from the above service #}
{% set randomServiceImage = service.homepageImage.order('RAND()').first() %}

{% if randomServiceImage %}
    &lt;img src="{{ randomServiceImage.getUrl('imageTransformName') }}" alt=""&gt;
{% endif %}
</code></pre>

<p>For more info, have a look here: <a href="https://craftcms.com/docs/templating/craft.entries#order" rel="nofollow">https://craftcms.com/docs/templating/craft.entries#order</a></p>

<p>Let us know how you get on :)</p>

CC BY-SA 3.0
2016-07-14T10:19:07.260Z
Fair enough, cheers Mats!
Matt P
CC BY-SA 3.0
2016-07-14T10:25:49.323Z
Yeah, the `for` loops weren't neccessary/would fail as you're using `first()` :) Could do with a conditional testing the `randomService` variable, but otherwise this looks legit – assuming `left` is a CategoryModel instance, of course :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-14T21:17:32.197Z
I'm beginning to understand the code above. However, when you say "assuming left is a CategoryModel instance" what do you mean?

I have created a Category called homepageView and then created two categories in it called right and left. 

I then created a category field called homepageImageLayout and added it to my services channel. I selected left in one of the entries.
bjyama
CC BY-SA 3.0
2016-07-15T07:44:32.460Z
@bjyama `.relatedTo(left)` will only work if the variable `left` is actually set to a category (i.e. an instance of [CategoryModel](https://craftcms.com/docs/templating/categorymodel)). By itself, using `left` will only throw an error (undefined variable). From your description, I'm don't really understand how you structured this so I'm unable to give you a good example, but you'll need something like `{% set left = craft.categories.group('yourCategoryGroupHandle').slug('left').first() %}` somewhere in your template (before the `{% set randomService...` part).
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-07-14T08:01:29.957Z
2016-07-14T08:11:27.770Z
2016-07-14T08:11:27.770Z
Matt P
<p>I am trying to pull an image from services marked with a category of left. I would only like to display one image. I would also like to randomize the image if possible from all the images marked left. </p>

<pre><code>{# RANDOM IMAGE FROM SERVICES - LEFT #}
&lt;div class="grid-item"&gt;
    {% for homepage in craft.entries.section('services').limit('1').relatedTo(left).find() %}
        &lt;div class="image-wrapper overlay-fade-in"&gt;

            &lt;img src="{{ homepage.homepageImage.first().getUrl }}" alt="{{ homepage.title }}"/&gt;

            &lt;div class="image-overlay-content"&gt;

                &lt;h2&gt;{{ homepage.title }}&lt;/h2&gt;
                &lt;p&gt;{{ homepage.description }}&lt;/p&gt;
                &lt;a href="services.html" class="button"&gt;View Rental Pricing&lt;/a&gt;

            &lt;/div&gt;
        &lt;/div&gt;
    {% endfor %}
&lt;/div&gt;
</code></pre>

<entries><categories><random>
Pull random image from entries marked with a certain category
# Pull random image from entries marked with a certain category

(Asked by: username_0 on 2016-07-14)

I am trying to pull an image from services marked with a category of left. I would only like to display one image. I would also like to randomize the image if possible from all the images marked left. 
{# RANDOM IMAGE FROM SERVICES - LEFT #}

    {% for homepage in craft.entries.section('services').limit('1').relatedTo(left).find() %}
        

            

            

                {{ homepage.title }}
                {{ homepage.description }}
                View Rental Pricing

            
        
    {% endfor %}

**Tags:** entries, categories, random


---

## 1 answers

### Answer by username_1 on 2016-07-14. Score: 3

Welcome to the Craft community @username_0
{# Step one: pick a random service #}
{% set randomService = craft.entries.section('services').relatedTo(left).order('RAND()').first() %}

{# Step two: pick a random image from the above service #}
{% set randomServiceImage = service.homepageImage.order('RAND()').first() %}

{% if randomServiceImage %}
    
{% endif %}

For more info, have a look here: https://craftcms.com/docs/templating/craft.entries#order
Let us know how you get on :)

> **Comments:**
> **username_1**: Fair enough, cheers Mats!  
> **username_2**: Yeah, the `for` loops weren't neccessary/would fail as you're using `first()` :) Could do with a conditional testing the `randomService` variable, but otherwise this looks legit – assuming `left` is a CategoryModel instance, of course :)  
> **username_0**: I'm beginning to understand the code above. However, when you say "assuming left is a CategoryModel instance" what do you mean?  I have created a Category called homepageView and then created two categories in it called right and left.   I then created a category field called homepageImageLayout and added it to my services channel. I selected left in one of the entries.  
> **username_2**: @username_0 `.relatedTo(left)` will only work if the variable `left` is actually set to a category (i.e. an instance of [CategoryModel](https://craftcms.com/docs/templating/categorymodel)). By itself, using `left` will only throw an error (undefined variable). From your description, I'm don't really understand how you structured this so I'm unable to give you a good example, but you'll need something like `{% set left = craft.categories.group('yourCategoryGroupHandle').slug('left').first() %}` somewhere in your template (before the `{% set randomService...` part).  

---

