0

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}

1 Answer 1

0

Insert the post ID in the_field function. See the documentation.

<?php the_field($field_name, $post_id); ?>
  • $field_name: the name of the field to be retrieved. eg “page_content” (required)
  • $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

So in your case use the_field('tour_length', $item->ID) to retrieve the correct data for the specific post.

2
  • Thank you, that works in the search loop but not in the category loop... any idea why? Commented Feb 4, 2015 at 10:09
  • @CiaranGaffey Did you add the field to the category? Is that even possible with ACF? If so than I think it's still the same logic only now you should use the category ID instead of the post ID.
    – luukvhoudt
    Commented Feb 4, 2015 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.