Figured out how to annotate links to PDF files

Sharing a small victory:

I wanted to check links for a file extension so I could automatically annotate links to PDFs. I found some older posts fielding similar questions, but I had to combine tips and experiment until I got my scenario working.

In my case, different categories of files are uploaded to the site using respective templates, so that’s how I’m pulling them into separate lists for display.

I’ve just added a bit of text to each link, but you could use an icon:

<h2>Shareholder Reports</h2>
  <ul>
    <?php foreach($page->files()->template('reports')->sortBy('sort', 'asc') as $item): ?>
      <li>
        <a href="<?= $item->url() ?>"><?= $item->title()->esc()->or($item->filename()) ?>
          <?php if ($item->extension() == 'pdf'): ?>
            <i>(pdf)</i>
          <?php else: ?>
            <i>(other)</i>
          <?php endif ?>
        </a>
      </li>
    <?php endforeach ?>
  </ul>
2 Likes