I am trying to create a download link (browser downloads link on click) from a file field within a repeater.
When a file field is set to ID, this works fine using the example code:
$file = get_field(‘file’);
if( $file ):
$url = wp_get_attachment_url( $file ); ?>
” >Download File
<?php endif;
However, setting the file field to return an array, the <?php echo $file[‘url’]; ?> and <?php echo $file[‘id’]; ?> only links to the post page or attachment.
I need both a link to the attachment page and a link to direct download.
Can someone advise me if this is possible and share an example code? This is my current working code linking to the attachment page only.
<?php
// Check rows exists.
if( have_rows(‘game_assets’) ):
// Loop through rows.
while( have_rows(‘game_assets’) ) : the_row();
// Load file field value.
$file = get_sub_field(‘game_file’ );
if( $file ):
if( $file[‘type’] == ‘image’ ) {
$icon = $file[‘sizes’][‘thumbnail’];
}
// Begin caption wrap.
if( $caption ): ?>
<div class=”wp-caption”>
<?php endif; ?>
<div class=”file-wrap”>
<div class=”file-content”>
<div class=”file-image”>” width=”20″ height=”auto”/></div>
<div class=”file-text-wrap”>
<div class=”file-name”><?php echo $file[‘filename’]; ?></div>
<div class=”file-type”><p>File type: <?php echo $file[‘type’]; ?></p></div></div>
</div>
<div class=”download-wrap”>
” target=”_blank”>Download
</div>
</div>
<?php endif;
// End loop.
endwhile;
// No value.
else :
// Do something…
endif; ?>