Fetch and filter by image meta data


#1

For content inside my “blog”-directory, I would like to (1) fetch and output image meta data, also I would like to (2) filter by image meta data.

Here is the code I have:

    <?php foreach($site->find('blog')->children()->limit(10) as $article): ?>
  <?php if($image = $article->image()): ?>
    <figure>
      <img src="<?= $image->url() ?>" alt="<?= $image->title() ?>">
    </figure>
  <?php endif ?>
    <p><a href="<?php echo $article->url() ?>"><?php echo $article->title()->html() ?></a>
      <br><?php echo $article->description() ?></p>
<?php endforeach ?>

(1) filtering

The meta data by which I would like to filter is stored inside a meta data file. Specifically, it looks like this:

Category: product-image

So, I would like to fetch images only with “product-image” as their category.

(2) output meta data

Sadly, this part doesn’t output anything:

alt="<?= $image->title() ?>"

I don’t know why. Can you help? Thank you.


#2

You are only fetching a single image, so you can’t filter this. You would have to get all images

$images = $article->images();

Then you can filter, e.g.

$images = $images->filterBy('category', 'product-image');
<?= $image->title() ?>

is correct if the image has a title field.


#3

Thank you so much.

I have tried this. But I think my code where I combined all these commands is wrong, my site gets an error. Could you spell out the code?


#4

What is the error message and in what line is it thrown? Please provide your code, it’s less work to correct it.


#5

The error is:

This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.

My code is:

<?php foreach($site->find('blog')->children()->limit(10) as $article): ?>
  <?php $images = $article->images(); ?>
  <?php $images = $images->filterBy('category', 'product-image'); ?>

    <figure>
      <img src="<?= $image->url() ?>" alt="<?= $image->title() ?>">
    </figure>

    <p><a href="<?php echo $article->url() ?>"><?php echo $article->title()->html() ?></a>
      <br><?php echo $article->description() ?></p>
<?php endforeach ?>

Thank you!


#6

Please enable debugging in your config.php:

c::set('debug', true);

#7

The error is called:

Call to a member function url() on null

This line is being marked:

<img src="<?= $image->url() ?>" alt="<?= $image->title() ?>">

And my code currently looks like this:

<?php foreach($site->find('blog')->children()->limit(10) as $article): ?>
  <?php $images = $article->images()->filterBy('category', 'product-image'); ?>
  
    <figure>
      <img src="<?= $image->url() ?>" alt="<?= $image->title() ?>">
    </figure>

    <p><a href="<?php echo $article->url() ?>"><?php echo $article->title()->html() ?></a>
      <br><?php echo $article->description() ?></p>
<?php endforeach ?>

#8

Ah ok, of course, if you deal with a collection of files, you have to loop through them in a foreach loop:

foreach($images as $image) {
  // 
}

otherwise, PHP doesn’t know what $image is and the call on url doesn’t work.


#9

Thank you for your help. So like this?

<?php
foreach($images as $image) {
  <img src="<?php echo $image->url() ?>">
}
?>

I can’t make it work. I get:

ParseError
syntax error, unexpected ‘<’

for this line:

<img src="<?php echo $image->url() ?>">


#10

You can’t use HTML tags inside PHP curly braces/use php tags inside php tags:

<?php foreach($images as $image): ?>
  <img src="<?php echo $image->url() ?>">
<?php endforeach ?>

#11

Thank you! It works. Many thanks.