Add files to either a <VIDEO> or <IMG> tag

Hi —

What would be the best way to identify each file format within a page and place and display it with the correct tag.

ex: if it’s a MP4 file i want it to go into a or it’s a JPG I want it to go into a tag?

 <?php foreach($page->files() as $item): ?>

<?php if($video = $item->file('JPG')): ?>
       <img src="<?php echo $item->url() ?>">
<?php endif ?>

<?php if($image = $item->file('MP4')): ?>
       <video width="320" height="240" "><source src="<?php echo $item->url() ?>" type="video/mp4"></video>
<?php endif ?>

      <?php endforeach ?>

very thankful for any tips on this!

xx

Use $file->type():

<?php foreach($page->files() as $item): ?>

  <?php if($item->type() == 'image'): ?>
       <img src="<?php echo $item->url() ?>">
  <?php endif ?>

  <?php if($item->type() == 'video'): ?>
       <video width="320" height="240" "><source src="<?php echo $item->url() ?>" type="video/mp4"></video>
  <?php endif ?>

<?php endforeach ?>

Another way of doing it without an if statement would be to create two snippets with the tags for image/video (image.php and video.php) and call these depending on file type:

<?php foreach($page->files() as $file): ?>
  <?php snippet($file->type()) ?>
<?php endforeach ?>
1 Like