Calling an image with more than one Checkboxes selected?

Hi there

I have a list of images in a Case Study page. I need to show one of them on the Home page (‘home’) and another on a Projects page (‘projects’).

In the file blueprint, I created a field with Checkboxes called ‘Template’

fields:
  template:
    label: Template
    type: checkboxes
    options:
      home: Home
      projects: Projects
    help: '(this image will show on the Home and/or Projects page if selected)'

In some instances, the one image will need to show on both Home and Projects pages.

The code works when one Template is checked and an image is called. For example:

<?php if($home = $caseStudy->images()->template('home')->sortBy('sort')->first()): ?>
  <?= $home ?>
<?php endif ?>

or

<?php if($projects = $caseStudy->images()->template('projects')->sortBy('sort')->first()): ?>
  <?= $projects ?>
<?php endif ?>

However, when both are checked (“home, projects”) the image does not show at all.

My question is how can I call an image with both Checkboxes selected?

Much appreciate your help :pray:

template is a special field in a file blueprint that is reserved for file blueprints, and the template() filter method refers to that.

So:

  1. Rename your field to something else (pageTemplate, for example)
  2. Don’t use template for filtering, but the filterBy() method,
$image = $caseStudy->images()->filterBy('pageTemplate', 'home', ',')->sortBy('sort')->first();
1 Like

Oh great that seems to do the trick. Thank you so much!