Home › Forums › General Issues › how to get filetype (extension)? › Reply To: how to get filetype (extension)?
Here’s how I’ve done it. I haven’t tested uploading every file type but the ones I have tried do work. This is using material design icons.
<?php if( have_rows( 'download_list_clone_download_groups' ) ): ?>
<section class="download-lists">
<?php while( have_rows( 'download_list_clone_download_groups' ) ): the_row(); ?>
<article class="download-list">
<h3 class="download-list__title"><?php the_sub_field( 'group_title' ); ?></h3>
<?php if( have_rows( 'downloads' ) ): ?>
<ul>
<?php while( have_rows( 'downloads' ) ): the_row(); ?>
<?php $attachment_id = get_sub_field('file'); ?>
<?php $attachment_url = wp_get_attachment_url($attachment_id); ?>
<?php $pathinfo = pathinfo( get_attached_file($attachment_id)); ?>
<?php $filetype = $pathinfo['extension']; ?>
<?php
switch ($filetype) {
case "pdf":
$fileclass = 'file-pdf';
break;
case "jpg":
case "jpeg":
case "png":
case "svg":
$fileclass = 'file-image';
break;
case "pptx":
case "pptm":
case "ppt":
$fileclass = 'file-powerpoint';
break;
case "xlsx":
case "xlsm":
case "xls":
$fileclass = 'file-excel';
break;
case "doc":
case "docm":
case "docx":
case "odt":
case "rtf":
case "txt":
$fileclass = 'file-word';
break;
default:
$fileclass = 'file-document';
break;
}
?>
<li class="download-list__download"><a href="<?php echo $attachment_url; ?>"><i class="mdi mdi-<?php echo $fileclass; ?>"></i><span><?php the_sub_field( 'download_title' ); ?></span><i class="mdi mdi-download"></i></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</article>
<?php endwhile; ?>
</section>
<?php endif; ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.