There is a loop in my theme that displays posts with the following information: a title, some text, and a price. I have added a custom field to this post type using ACF. The field ID is tour_length
. I add the custom field value to the loop using <?php the_field('tour_length'); ?>
however when the posts display on the front end it only displays the value for the first item and repeats it for every subsequent item.
{foreach $posts as $item}
{first}<ul class="items">{/first}
<li class="item clear{ifset $item->packageClass} {$item->packageClass}{/ifset}{ifset $item->optionsDir['featured']} featured{/ifset}">
{if $item->thumbnailDir}
<div class="thumbnail">
<a href="{!$item->link}"><img src="{thumbnailResize $item->thumbnailDir, w => 155, h => 115}" alt="{__ 'Item thumbnail'}"></a>
</div>
{/if}
<div class="description">
<div class="info">
{var $lp = getTourPrice($item->ID)}
{if $lp}<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>{/if}
// HERE IS WHERE I'VE ADDED MY CUSTOM FIELD
<div class="item-length"><span><?php the_field('tour_length'); ?><span class="days">Days</span></span></div>
</div>
<div class="desc-head">
<h3><a href="{!$item->link}">{$item->post_title}</a></h3>
</div>
<div class="desc-text">
{if shortcode_exists( 'loop' )}
{doShortcode "[loop id=".$item->ID."]"}
{/if}
{!$item->excerptDir}
</div>
<a href="{!$item->link}" class="trip-order enquire-btn read-more">Learn More</a>
</div>
<!-- tour offers -->
{ifset $GLOBALS['findedOffers'][$item->ID]}
{if count($GLOBALS['findedOffers'][$item->ID]) > 0}
<table class="item-offers">
{foreach $GLOBALS['findedOffers'][$item->ID] as $offer}
<tr class="offer">
<td class="offer-title"><a href="{add_query_arg(array('offer' => $offer->ID),$item->link)}">{$offer->post_title}</a></td>
<td class="offer-date">{$offer->from} - {$offer->to}</td>
<td class="offer-price">{$offer->price}</td>
</tr>
{/foreach}
</table>
{/if}
{/ifset}
</li>
{last}</ul>{/last}
{/foreach}
Can someone point me in the right direction as to why that's happening?
EDIT: Implementing the solution as pointed out by Fleuv solved the issue for posts being displayed in a search. However posts being displayed in the category still show the problem. Here is the category loop:
{foreach $posts as $item}
{first}<ul class="items">{/first}
<li class="item clear{ifset $item->packageClass} {$item->packageClass}{/ifset}{ifset $item->optionsDir['featured']} featured{/ifset}">
{if $item->thumbnailDir}
<div class="thumbnail">
<a href="{!$item->link}"><img src="{thumbnailResize $item->thumbnailDir, w => 155, h => 115}" alt="{__ 'Item thumbnail'}"></a>
</div>
{/if}
<div class="description">
<div class="info">
{var $lp = getTourPrice($item->id)}
{if $lp}<div class="item-price"><span><span><span class="from">From</span>{$lp}</span></span></div>{/if}
// MY CUSTOM FIELD
<div class="item-length"><span><?php the_field('tour_length', $item->ID); ?><span class="days">Days</span></span></div>
</div>
<div class="desc-head">
<h3><a href="{!$item->link}">{$item->title}</a></h3>
</div>
<div class="desc-text">
{if shortcode_exists( 'loop' )}
{doShortcode "[loop id=".$item->id."]"}
{/if}
{!$item->excerpt}
</div>
<a href="{!$item->link}" class="trip-order enquire-btn read-more">Learn More</a>
</div>
</li>
{last}</ul>{/last}
{/foreach}