I created a page where users can download files.
Demo: http://sciencewalden.org/document
<?php foreach ($page->children()->listed() as $item): ?>
<a href="<?= $item->images() ?>" download>
<?php endforeach ?>
All files can be downloaded, but only the ‘000.pdf’ file is downloaded as ‘000.html’.
What is the problem?
Should I modify the toolkit in the kirby folder?
Please help me if someone knows the answer.
Where are the pdf files? You are trying to print all images of the given page as href attribute which. doesn’t make sense.
The button with “4 pdftest” doesn’t link to a pdf file. Actually it only links to ""
.
The HTML for that part comes out as:
<li class="before-results">
<span class="numbering">4</span><span>pdftest</span>
<a href="" download><span class="downbtn">Download</span></a>
</li>
While the other buttons all have a valid href
attribute.
How are you producing the URL for the link to the pdf? Can we see that part of the PHP template?
Thank you for your kind answer.
I have uploaded all the files, the other files work fine but only the pdf doesn’t work.
(The pdf file is also uploaded.)
The php code is here.
document.php
<?php
foreach ($page->children()->listed() as $item): ?>
<li class="before-results">
<span class="numbering"><?php echo $item->num() ?></span><span><?= $item->title() ?></span>
<a href="<?= $item->images() ?>" download><span class="downbtn">Download</span></a>
</li>
<?php endforeach ?>
document-item.yml = $item
(This is the blueprint of the child page in the document.)
title: document-item
icon: 📁
preset: page
pages: false
fields:
images:
label: 파일
type: files
layout: cards
template: image
query: page.images
info: "{{ file.dimensions }}"
image:
ratio: 5/4
cover: true
min: 1
size: small
uploads: false
document-item.php
(This page is not decorated because it is an invisible page.)
<?php
?>
<?php snippet('header') ?>
<main>
<?php snippet('intro') ?>
<div class="text">
<?= $page->text()->kt() ?>
</div>
</main>
<?php snippet('footer') ?>
coolcoolsuess:
images:
Ah, ok, so the field where you select the PDF is called images
. But that field name clashes with Kirbys $page->images()
method which fetches all images from the page. You have to either rename the field or call it via the content method
<?php foreach ( $page->content()->images() as $image ) : ?>
<!-- do stuff here -->
<?php endforeach ?>
Or if you have only one file selected:
<?php
foreach ($page->children()->listed() as $item): ?>
<li class="before-results">
<span class="numbering"><?php echo $item->num() ?></span><span><?= $item->title() ?></span>
<?php if ( $file = $page->content()->images()->toFile() ) : ?>
<a href="<?= $file->url() ?>" download><span class="downbtn">Download</span></a>
<?php endif ?>
</li>
<?php endforeach ?>
If you want to have only one file, I’d limit the field to max: 1
. On a side note: I’m a big fan of naming fields/stuff in general according to what they contain, because images
for a (single) document is a bit misleading. I’d also change the query
to query documents rather than images…
1 Like
Thank you very much for your kind answer.
I solved the problem by creating two query pages.
I have one more question.
“hwp” is not uploaded as “query. documents” or “query.images”. What query should I write for hwp?
coolcoolsuess:
hwp
I must admit that I had to google this document extension first . And yes, this extension is not among image or document files as defined in Kirby.
Document file types: $page->documents() | Kirby CMS
You can find all defined types in /kirby/src/Toolkit/F.php
.
1 Like