Support

Account

Home Forums Add-ons Repeater Field Loops files with URL

Solved

Loops files with URL

  • Hey all

    I have the following code which loops through files added to a repeater field. I have been trying to make it work for hours 🙁

    		 <?php
     $rows = get_field('files-ra');
    if($rows) {
     
     
        foreach($rows as $row) {
        
        $thisTitle = trim($row['file_name']);
        if($thisTitle == "") {
        $thisTitle = "Download this whitepaper";
        }
        
            echo '<li><a href="'.$row['file']['url'].'">'.$thisTitle.'</a></li>';
            
            
        } 
    }
     
    ?>

    …but it’s not working properly.

    The name is output, but the link is just the first letter of the filename. Very odd. Anything wrong with this loop to you?

    Screenshot of my fields here: http://i.imgur.com/hV6WV40.png

    Thanks for any guidance.
    Martin

  • I just did the same thing, below is how I did it. Since Elliot added the have_rows function in 4.3 (http://www.advancedcustomfields.com/resources/functions/have_rows/) it’s much easier to get it to work. My code might not be optimal (been up too long…), but it works.

    <?php
    
      $rows = get_field('reaction_videos');
    
      if($rows) {
    
        echo '<h3 class="reaction">Reaction Videos</h3>';
        echo '<ul>';
    
        while(the_sub_field('reaction_videos')) {
    
              $video = the_sub_field('reaction_video');
              $desc = the_sub_field('reaction_video_desc');
    	
              echo '<li><a href="' . $video . '" title="' . $desc . '" class="lightbox" rel="foobox">' . $desc . '</a></li>' ;
    
    	  }
    
      }
    
    ?>
  • Hi Nuro. Thanks.

    This isn’t working though. The output is just blank, and I definitely substituted your field names with mine:

     <?php
    
      $rows = get_field('files-ra');
    
      if($rows) {
    
        while(the_sub_field('files-ra')) {
    
              $file = the_sub_field('file');
              $name = the_sub_field('file_name');
    	
              echo '<li><a href="' . $file . '" title="' . $name . '">' . $name . '</a></li>' ;
    
    			  }
    
    		  }
    
    		?>

    It might be worth noting that earlier in the document is
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    I need that still right?

    Such confuse.

  • OK, fixed it. Used this to output various details from file uploads:

    <?php if( have_rows('files-ra') ): ?>
     
    	
    	<?php while( have_rows('files-ra') ): the_row(); 
     
    		$file = get_sub_field('file');
    		$name = get_sub_field('file_name');
     
    		?>
     
    		<li>
     			<a href="<?php echo $file['url']; ?>"><?php the_sub_field('file_name'); ?></a>
    		</li>
     
    	<?php endwhile; ?>
     
    	</ul>
     
    <?php endif; ?>

    Thanks for the help

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

You must be logged in to reply to this topic.