Kirby

Targeting some specific images

Hello,

I’m trying to show on my home-page some specific images (rather that just one image as a cover). In my blueprint, I chance the cover entries, as a multiple, however, when I run the code in my homepage, it’s still show only one image instead of multiples ones.

Anyone have any leads how I can target some specific images in my album and show it randomly in my homepage?

Your question is a little contradictory, and you dont say which version of Kirby you are using? There are several ways to do what I think you are asking, but I need to know the Kirby version to answer correctly. With Kirby 3 you can use a files section and a files field to upload the images and set the them into a gallery field. Then in your template you use shuffle() and limit() to load a random one from a specific list of images.

With kirby 2, it is probably easiest to use the images plugin, in combination with shuffle() and limit(). Alternatively you can name the files with a prefix and use filterby to find them and show just one, again using shuffle and limit.

hi thank you for taking the time of answer.
I’m using Kirby 3.

I know how to display images in the gallery, and how to shuffle them, but I’ll try to explain in more details what I’m trying to achieve.
with kirby 3, in your home page, you are to display a cover picture of your album. what i would like to do is display multiple (but selected) cover of this album in the home page randomly. I tried to chance the blueprint, but when i run the code only one images will be display.

please let me know if still sound confusion.

Sounds like your basing a site on the starter kit?

You probably need to set a field in the image meta (lets call it featured)… then you filter those in your template.

inside the loop for your images, you would do something like this, assuming you used the toggle field…

<?php if($image->featured()->bool()): ?>
  <!-- image markup -->
<?php endif ?>

Changing the blueprint wont help much, since its the PHP templates and snippets that dictate how things show up on the site. The blue prints just dictate how things are stored.

Welcome to the forum by the way, i see you are a new user :slight_smile:

gotcha.

I thought there was an easier way to directly select some images from the panel control, instead of changing their metadata.

I will try it, thank you!

Kirby tries not to dictate anything. The only required field is the title field, since it uses that to generate a URL for the page. Anything else is up to you. The Starterkit is a basic setup showing most of the main features to help people get up and running and understand how kirby works.

If you used the plainkit instead of the starter kit, you will see Kirby from scratch, meaning you have to setup everything how you like it.

But please, if you get stuck further, just ask :slight_smile:

Btw, you could use query language to pull images from any page into a field, and use that to base random from. That way would mean that, yes, you can do it visually in the panel. You would not need the toggle field in the meta data then.

Assuming that you have already changed the cover field in /site/blueprints/pages/album.ymlto accept multiple images instead of only one, you can fetch those images like this:

foreach ($albums as $album) {
  $images = $album->cover()->toFiles();
  foreach ($images as $image) {
    // do sth. with image
  }
}

Note however that in the Starterkit, there is a cover() method defined in a page model (’/site/models/album.php) that you would have to remove to make the above code work.

Yes! That’s exactly what I was trying to do! thank you so much

I think I’m still doing something not right:

<?php foreach (page('photography')->children()->listed() as $album): ?>
    <div class="this">
      <a href="<?= $album->url() ?>">
        <figure>
          <?php foreach ($images = $album->cover()->toFiles()): ?>
          <?= $images ?>
          <?php endforeach?>
          </figure>
      </a>
    </div>
     <?php endforeach ?>

Your syntax in the foreach loop is not correct:

<?php foreach ($album->cover()->toFiles() as $image): ?>
  <?= $image ?>
<?php endforeach?>