Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-14T10:19:07.260Z
Answers.Text: Fair enough, cheers Mats!
Answers.UserDisplayName: Matt P
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-14T10:25:49.323Z
Answers.Text: 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 :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-14T21:17:32.197Z
Answers.Text: 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.
Answers.UserDisplayName: bjyama
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-15T07:44:32.460Z
Answers.Text: @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).
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-14T08:01:29.957Z
Answers.LastActivityDate: 2016-07-14T08:11:27.770Z
Answers.LastEditDate: 2016-07-14T08:11:27.770Z
Answers.UserDisplayName: Matt P
Body: <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>
  
Tags: <entries><categories><random>
Title: Pull random image from entries marked with a certain category
ThreadText: # 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 #}
  <div class="grid-item">
      {% for homepage in craft.entries.section('services').limit('1').relatedTo(left).find() %}
          <div class="image-wrapper overlay-fade-in">
  
              <img src="{{ homepage.homepageImage.first().getUrl }}" alt="{{ homepage.title }}"/>
  
              <div class="image-overlay-content">
  
                  <h2>{{ homepage.title }}</h2>
                  <p>{{ homepage.description }}</p>
                  <a href="services.html" class="button">View Rental Pricing</a>
  
              </div>
          </div>
      {% endfor %}
  </div>
  
  ```
  
  **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 %}
      <img src="{{ randomServiceImage.getUrl('imageTransformName') }}" alt="">
  {% 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 username_2!  
  > **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).  
  
  ---
  
  