Support

Account

Home Forums Add-ons Repeater Field Attachment Size with Repeater Field issue

Solved

Attachment Size with Repeater Field issue

  • With the basic Advanced Custom Fields, File Upload (Selecting “ID”), the code below successfully posts the file size of the attachment. However, I need this to work with ACF Repeater and I can’t figure out where to put what in order to make it work dynamically.

    <?php 
    //This block of code makes file name + file size appear with basic ACF	
    	
    $attachment_id = get_field('file_upload');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    		
    // part where to get the filesize
    $filesize = filesize( get_attached_file( $attachment_id ) );
    $filesize = size_format($filesize, 2);
    		
    // show custom field
    if (get_field('file_upload')): ?>
    <?php echo $title; ?> - <?php echo $filesize; ?>
    <?php endif; ?>

    And this is where I’m at with ACF Repeater Fields (adapted loop from http://www.advancedcustomfields.com/resources/field-types/repeater/)

    <?php
    
    $attachment_id = get_field('file_upload');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
    		
    // part where to get the filesize
    $filesize = filesize( get_attached_file( $attachment_id ) );
    $filesize = size_format($filesize, 2);
    
    // check if the repeater field has rows of data
    if( have_rows('repeater_field_name') ):
     
     	// loop through the rows of data
        while ( have_rows('repeater_field_name') ) : the_row();
    ?>
     
      <!-- display a sub field value -->
      <div class="item-upload">
        the_sub_field('file_upload');
      </div>
     
    <?php endwhile; else :
    // no rows found
    endif;
    ?>
  • Something like this.

    <?php if( have_rows('repeater_field_name') ): ?>
     	<?php while ( have_rows('repeater_field_name') ) : the_row(); ?>
     	
     	<?php
     		$attachment_id = get_sub_field('file_upload');
    		$url = wp_get_attachment_url( $attachment_id );
    		$title = get_the_title( $attachment_id );
    		
    		// part where to get the filesize
    		$filesize = filesize( get_attached_file( $attachment_id ) );
    		$filesize = size_format($filesize, 2);
     	?>
    
      	<!-- display a sub field value -->
      	<div class="item-upload">
        	<?php the_sub_field('file_upload'); ?> (<?php echo $filesize; ?>)
      	</div>
     
    <?php endwhile; else : ?>
    <?php endif; ?>
  • Thanks 1983ron! This worked perfect! Modified it a little, but it did exactly what I wanted.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.