I’m trying to make a simple document management system with ACF's repeater field. I need to print a button to download a file attached to the the top repeater field (with the size and filetype of the download). But if the top repeater field is empty, it should print “file not available” content.
I’m pretty new to PHP but this mostly works so far:
$repeater = get_field( 'document' )[0];
if( $repeater ) {
$fileurl = $repeater[ 'document' ][ 'url' ];
$filesize = filesize( get_attached_file ($repeater[ 'file' ][ 'id' ]) );
$filesize = size_format($filesize);
$filetype = wp_check_filetype( get_attached_file ($repeater[ 'file' ][ 'id' ]));
$download = '<div><a href="' . $repeater[ 'file' ][ 'url' ] . '">Download</a><div>' . $filesize . ' <span>' . $filetype[ 'ext' ] .'</span></div></div>' ;
echo $download;
}
This prints a button to the attached file in the top repeater, when there is an attached file in the top repeater. Only it prints out a dead link if there is nothing in the top repeater. This won’t do. I need to add an else condition or something so that it prints "file not available" content if there is nothing in the first repeater.
if(empty( $repeater )) {
$unavailable = '<div>Unavailable<div>This document isn\'t ready yet. Please check back later.</div></div>' ;
echo $unavailable;
}
I've tried a lot of different ways to do this, such as above, and I don't know what I'm doing wrong. Can you help?