I have an asset field (dataCard) which I use to upload a single PDF file tied to an entry. I'm trying, unsuccessfully, to display the asset tied to the entry on a page listing multiple entries. How would I do this? This is what I have so far:

{% set asset = entry.dataCard.first() %}

{% if asset %}
    <a href="{{ asset.url }}">{{category.title}}</a>
{% endif %}

What do you mean by 'display the asset'? Showing a thumbnail is not an option, because no thumbs are made for PDF files. If you'd want to show the filename, for example, use the filename property of the asset.

If the page contains multiple entries, you'd have to loop through the entries first. Then, for each entry, check if it has an asset in the desired field (by using | length, after which you can display the asset.

{% for entry in craft.entries.section('bla').relatedTo('bla') %}
    {% if entry.dataCard | length %}
        {% set asset = entry.dataCard.first() %}
        <a href="{{ asset.url }}">{{ asset.filename }}</a>
    {% endif %}
{% endfor %}
  • FYI - you can use PThumb or pdfthumbnails if your server supports ImageMagick. This allows you to create thumbnails for PDFs. – crawf May 7 '15 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.