Home › Forums › Add-ons › Repeater Field › Display Title of File in href using Repeater
I’m having trouble implementing the get_the_title for attachement_id. I’ve tried as per the code below to no avail. It works with single file fields, but when I use a Repeater it stops working.
How do I possibly display the title of an uploaded file, so I can use it in a href tag?
My Repeater field is called “add-audio-file” and the file field underneath is called “add_audio_file_repeater”
<?php
$attachment_id = get_field('add_audio_file');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );?>
<?php if(get_field('add_audio_file')): ?>
<ul>
<?php while(has_sub_field('add_audio_file')): ?>
<?php if( get_sub_field('add_audio_file_repeater') ): ?>
<li><a href="<?php the_sub_field('add_audio_file_repeater'); ?>" class="wpaudio"><?php echo $title;?></a></li>
<?php endif; ?>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hi @udapud
I can see from your code that the repeater field is called add_audio_file
.
Why then do you have the code:
$attachment_id = get_field('add_audio_file');
This is not the attachment id, it is the repeater field array…
The attachment ID is found WITHIN the loop via:
$attachment_id = get_sub_field('add_audio_file_repeater');
But, now looking at your field names, I think you have them around the wrong way. Please read the documentation before coding and asking questions, it is very simple to understand. I think you have just copied / pasted without looking at the actual code.
Thanks
E
I can confirm I am seeing “Array” as the href, however when I replace
get_field('add_audio_file')
with
$attachment_id = get_sub_field('add_audio_file_repeater');
It retrieves nothing. On top of that, for some reason there are 75 entries for adding a file from the moment I create a file repeater field.
Hi @udapud
I think your field names are incorrect. Perhaps you could post a screenshot of the field group edit page so I can see the repeater / sub fields
Also, a screenshot of the edit page would be good.
As for the 75 sub fields, this is most likely due to 1 of 2 things:
1. You have selected a minimum of 75 rows for the repeater field
2. You have a previously saved value with the same field_name. This would have saved the value 75 at some point
Hi @elliot
Screenshots attached, top level field is called “testlabel” with repeater. Subfield level is called “testsubfield” with file.
The file field “testsubfield” is set to File ID.
They are attached to display on a custom post type called “acts”. The code I’m using is below which includes your last replies suggestions. When I made this field it didn’t show 75 results show just ignoring the problem for now.
Thanks
<?php
$attachment_id = get_sub_field('testsubfield');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
?>
<?php if(get_field('testlabel')): ?>
<ul>
<?php while(has_sub_field('testlabel')): ?>
<li><a href="<?php echo $url; ?>" class="wpaudio"><?php echo $title; ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hi @udapud
Your code will not work because you are using get_sub_field OUTSIDE of the has_sub_field loop.
It MUST be used within the loop, otherwise, how would your code know which row to fetch the data from?!
Please read over the docs for more information about the functions you are using
This code using get_sub_field in the loop gives me the exact same results as the code above.
<?php
if(get_field('testlabel'))
{
echo '<ul>';
while(has_sub_field('testlabel'))
{
echo '<li><a href="' . $url . '" class="wpaudio">' . $title . '</a></li>';
}
echo '</ul>';
}
?>
Hi @udapud
Your code does not contain any get_sub_field function…
Please read over the documentation, it is very simple.
You can’t store $url OUTSIDE of the loop, that is because it is data within the ROW!
Thanks
E
I’m assuming for second that you realise that this is a WORDPRESS plugin that was made for WordPress users and developers, who don’t necessarily count themselves as php Gurus. I did pay for this product and as such require some support.
Can you please humour me with a working example please. I tried this below but received to values.
<?php
if(get_sub_field('testsubfield'))
{
$attachment_id = get_sub_field('testsubfield');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
echo '<ul>';
while(has_sub_field('testsubfield'))
{
echo '<li><a href="' . $url . '" class="wpaudio">' . $title . '</a></li>';
}
echo '</ul>';
}
?>
<?php
if(get_field('testrepeaterfield'))
{
echo '<ul>';
while(has_sub_field('testrepeaterfield'))
{
$attachment_id = get_sub_field('testsubfield');
$url = wp_get_attachment_url( $attachment_id );
$title = get_the_title( $attachment_id );
echo '<li><a href="' . $url . '" class="wpaudio">' . $title . '</a></li>';
}
echo '</ul>';
}
?>
Please humor me and read the documentation as suggested:
http://www.advancedcustomfields.com/resources/functions/has_sub_field/
Also, for your information:
The Advanced Custom Field’s API makes it very easy to display field data in your theme. However, you must be confident with PHP programing and able to customize a WordPress template. Here’s some useful links to get started:
Diving into PHP – http://blog.themeforest.net/screencasts/diving-into-php-day-2/
Diggin into WordPress – http://digwp.com/
Awesome Elliot thank you, I shall spend some time learning about has_sub_field loop. I really love this plugin, it has completely transformed the way I do business.
If you need more idea about integration of many possible variables and attrutes including the description and details that pertain to product information, you can check this one at http://eatmywords.com/portfolio/before-and-after/
thousands of examples but not any is helpful..
How can i get the title or maybe caption of the file?
The topic ‘Display Title of File in href using Repeater’ 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.