Home › Forums › Add-ons › Repeater Field › Trouble retrieving File URL in Repeater Field
I’ve added some fields to my products, including some PDF files that have been uploaded via the repeater field. But for some reason I can’t get the URL to appear in the href properly.
The code below results in everything except the link and document title from appearing (though the <li>
appears just fine).
<?php
// check for rows (parent repeater)
if( have_rows('downloads') ): ?>
<div id="downloads">
<?php
// loop through rows (parent repeater)
while( have_rows('downloads') ): the_row(); ?>
<div>
<h3><?php the_sub_field('downloads_title'); ?></h3>
<?php // check for rows (sub repeater)
if( have_rows('documents') ): ?>
<ul>
<?php
$document = get_sub_field('document_upload');
$url = $document["url"];
?>
<li><a href="<?php echo $url ?>"><?php the_sub_field('document_name'); ?></a></li>
<?php endif; //if( get_sub_field('documents') ): ?>
</ul>
<?php endwhile; // while( have_rows('downloads') ): ?>
</div>
<?php endif; // if( have_rows('downloads') ): ?>
Using the following code I at least get the document name to appear in the list, but the link itself doesn’t work at all.
<?php
// check for rows (parent repeater)
if( have_rows('downloads') ): ?>
<div id="downloads">
<?php
// loop through rows (parent repeater)
while( have_rows('downloads') ): the_row(); ?>
<div>
<h3><?php the_sub_field('downloads_title'); ?></h3>
<?php
// check for rows (sub repeater)
if( have_rows('documents') ): ?>
<ul>
<?php
// loop through rows (sub repeater)
while( have_rows('documents') ): the_row();
// display each item as a list - with a class of completed ( if completed )
?>
<li><a href="<?php the_sub_field('document_upload'); ?>"><?php the_sub_field('document_name'); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; //if( get_sub_field('documents') ): ?>
</div>
<?php endwhile; // while( have_rows('downloads') ): ?>
</div>
<?php endif; // if( have_rows('downloads') ): ?>
I just need to retrieve that URL somehow.
RELATED: Any chance someone can direct me on how to hook these fields into the Woocommerce Single Product page? Right now the code is simply added to the single-product.php, immediately following the woocommerce_content, but it’s not actually appearing where I want it to. I suspect I need to use some sort of hook with that code added to it.
The “Related” issue has been solved. But I still do need assistance on getting that file URL. I’ve even changed the sub_field return value to “File URL” (it had been “File Object”), but it’s still not working properly.
What do you have the return value set to for the file field? If it is file array (or file object depending on the version of ACF) then what you have should be working. If you’ve changed the return value is the only reason I can see that it’s not working.
http://www.advancedcustomfields.com/resources/file/
It is set to ‘File Object’. Using an example from the link you provided, I slightly modified the code, but it still doesn’t work:
<?php
// check for rows (parent repeater)
if( have_rows('downloads') ): ?>
<div id="downloads">
<?php
// loop through rows (parent repeater)
while( have_rows('downloads') ): the_row();
?>
<div class="downloads-title">
<h3><?php the_sub_field('downloads_title'); ?></h3>
<i class="downloads-border"></i>
<?php
// check for rows (sub repeater)
if( have_rows('documents') ):
?>
<ul class="fa-ul">
<?php
// loop through rows (sub repeater)
while( have_rows('documents') ): the_row();
// display each item as a list - with a class of completed ( if completed )
$file = get_sub_field('document_upload');
?>
<li><i class="fa-li fa fa-download"></i><a href="<?php echo $file['url']; ?>"><?php the_sub_field('document_name'); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; //if( get_sub_field('documents') ): ?>
</div>
<?php endwhile; // while( have_rows('downloads') ): ?>
</div>
<?php endif; // if( have_rows('downloads') ): ?>
I set up a field group using the code as a reference and inserted your code into the template without making any changes to it. The result of my test is here: http://www.ssird7.com.php53-14.dfw1-2.websitetestlink.com/2015/08/18/hello-world/ and it’s working for me.
Here is the exported field group I created:
and here is the code in the template, which is just a copy of yours
sorry, prematurely submitted that. Let’s try again.
I set up a field group using the code as a reference and inserted your code into the template without making any changes to it. The result of my test is here: http://www.ssird7.com.php53-14.dfw1-2.websitetestlink.com/2015/08/18/hello-world/ and it’s working for me.
Here is the exported field group I created:
<?php
if(function_exists("register_field_group"))
{
register_field_group(array (
'id' => 'acf_test-group',
'title' => 'test group',
'fields' => array (
array (
'key' => 'field_56aaccc8f9a22',
'label' => 'downloads',
'name' => 'downloads',
'type' => 'repeater',
'sub_fields' => array (
array (
'key' => 'field_56aacd07f9a23',
'label' => 'downloads_title',
'name' => 'downloads_title',
'type' => 'text',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
array (
'key' => 'field_56aacd10f9a24',
'label' => 'documents',
'name' => 'documents',
'type' => 'repeater',
'column_width' => '',
'sub_fields' => array (
array (
'key' => 'field_56aacd30f9a25',
'label' => 'document_upload',
'name' => 'document_upload',
'type' => 'file',
'column_width' => '',
'save_format' => 'object',
'library' => 'all',
),
array (
'key' => 'field_56aaceeb47f43',
'label' => 'document_name',
'name' => 'document_name',
'type' => 'text',
'column_width' => '',
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'formatting' => 'html',
'maxlength' => '',
),
),
'row_min' => '',
'row_limit' => '',
'layout' => 'row',
'button_label' => 'Add Row',
),
),
'row_min' => '',
'row_limit' => '',
'layout' => 'row',
'button_label' => 'Add Row',
),
),
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
'order_no' => 0,
'group_no' => 0,
),
),
),
'options' => array (
'position' => 'normal',
'layout' => 'no_box',
'hide_on_screen' => array (
),
),
'menu_order' => 0,
));
}
and here is the code in the template, which is just a copy of yours
if( have_rows('downloads') ): ?>
<div id="downloads">
<?php
// loop through rows (parent repeater)
while( have_rows('downloads') ): the_row();
?>
<div class="downloads-title">
<h3><?php the_sub_field('downloads_title'); ?></h3>
<i class="downloads-border"></i>
<?php
// check for rows (sub repeater)
if( have_rows('documents') ):
?>
<ul class="fa-ul">
<?php
// loop through rows (sub repeater)
while( have_rows('documents') ): the_row();
// display each item as a list - with a class of completed ( if completed )
$file = get_sub_field('document_upload');
?>
<li><i class="fa-li fa fa-download"></i><a href="<?php echo $file['url']; ?>"><?php the_sub_field('document_name'); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; //if( get_sub_field('documents') ): ?>
</div>
<?php endwhile; // while( have_rows('downloads') ): ?>
</div>
<?php endif;
The question is, why isn’t it working for you, and what are you getting if you’re not getting the right thing.
Try outputting the content of $file after you get it and see what’s being returned
$file = get_sub_field('document_upload');
var_dump($file);
The topic ‘Trouble retrieving File URL in Repeater Field’ is closed to new replies.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.