【问题标题】:Kirby CMS/php: Checking the file type in order to apply varying markup?Kirby CMS/php:检查文件类型以应用不同的标记?
【发布时间】:2018-06-09 12:18:19
【问题描述】:

使用 Kirby CMS,我试图提取页面的所有文件并对图像和视频应用不同的标记。在 Kirby 中,有 $file->type() 命令,我想我必须在 if 语句中使用它才能区分文件,但我当前的代码不起作用。我尝试的是这样的:

<?php
$page = page('PageTitle');
$page_files = $page->files();
?>

<div class="slider-container" id="slider#">

    <div class="slider">

        <?php foreach($page_files->sortBy('sort', 'asc') as $page_file): ?>

            <?php if ($page_file->type() == 'image') { ?>

                <div style="background: url('<?= $page_file->url() ?>'); background-size: cover; height: 100vh;"></div>

            <?php endif; ?>

        <?php endforeach ?>

    </div>

</div>

我做错了什么?

【问题讨论】:

    标签: php html if-statement foreach kirby


    【解决方案1】:

    我的问题的答案很明显,我犯了一个简单的语法错误,在 if 语句的第一行使用 { 而不是 :。这将是代码的正确工作版本,区分图像和视频:

    <?php if($file->type() == 'image'): ?>
      <!-- markup for image -->
    <?php endif ?>
    
    <?php if($file->type() == 'video'): ?>
      <!-- markup for video -->
    <?php endif ?>
    

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 2011-08-02
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多