I have a Matrix field with Image, Caption, and PDF sub-fields. I can output the Image and Caption fine, but am having no luck with the PDF file.

Here is the code:

{% for block in entry.seriesInformation %}
    {% if block.type == "bladeImage" %}

        {% set image = block.image.first() %}

        {% if image %}
            <img src="{{ image.getUrl('bladesdetail') }}" width="{{ image.getWidth('bladesdetail') }}" height="{{ image.getHeight('bladesdetail') }}" alt="{{ image.title }}">
        {% endif %}

    {% elseif block.type == "caption" %}

        {{ block.caption }}

    {% elseif block.type == "bladeBrochure" %}

        <p><a href="{{ bladeBrochure.url }}">Download &rarr;</a></p>

    {% endif %}
{% endfor %}
  • Hi Brett, welcome to the Craft Stack Exchange site! I’ve edited your question a bit to make it more clear, and removed your second question about creating a Blog. Please post that as a separate question so it can have its own direct set of answers. Oct 9 '14 at 17:06
  • Tried both neither works. It gives me. A URL of PDF/filename. But does not download the file.
    – user882
    Oct 10 '14 at 11:35

Assuming the field where your client can select the download is an asset field, you can use the following:

{% elseif block.type == "bladeBrochure" %}
    {% set download = block.download.first() %}
    {% if download %}
        <a href="{{ download.url }}">Click me to download</a>
    {% endif %}

Replacing block.download with the name of your field.


I'm assuming that your PDF field is an assets field? If so, it works exactly the same way as images, and you can output it using something like this:

{% for block in entry.matrixFieldHandle %}
    {% set pdf = block.pdfFieldHandle.first() %}
    <a href="{{pdf.getUrl}}">Text</a>
{% endfor %}`
  • Thanks for editing Brandon; for some reason the 4-space indent just wasn't turning it into code earlier! Oct 9 '14 at 20:27

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.