Home › Forums › Front-end Issues › Get field on index outside loop
Hello! First of all, sorry me for my bad English but isn’t my native language. I’m having a problem with a field not showing up on index.php of my site. I’ve a group of fields applied to post_tag taxonomy, and want to show the field av_slider (the URL of a image field) on index.php outside the loop.
The code I’ve right now is:
<?php $tags = get_tags(array(
'number'=> 3,
'orderby'=> 'count',
'order'=> 'ASC',));
$html = "<ul class='av-grid'>";
foreach ( $tags as $tag ) {
$tag_link = get_tag_link( $tag->term_id );
$html .= "<li style='background: url(". the_field('av_slider', $tag->term_id) .") no-repeat center; background-size: cover;'><a href='{$tag_link}' title='{$tag->name}'><div class='av-grid-title'>";
$html .= "{$tag->name}</div><span class='av-grid-icon av-icon-eye'></span></a></li>";
}
$html .= "</ul>";
echo $html; ?>
The problem is the_field(av_slider) is blank and can’t get it to display the image url.
Thanks a lot in advance! 🙂

the_field('av_slider', $tag->term_id)
should be
the_field('av_slider', $tag)
OR
the_field('av_slider', 'term_'.$tag->term_id)
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Awesome, thank you very much! It solved the problem and now can see the URL of image, but it renders outside background: url() tag.
On the front-end the URL appears before “ul” element. Don’t know why. :/

You want to use get_field() for the way that your using it. the_field() echos the value and get_field() returns the value which is what you want if you’re going to manipulate the value.
You must be logged in to reply to this topic.
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.