在我的主题中有一个循环,显示包含以下信息的帖子:标题、一些文本和价格。我已经使用ACF向此帖子类型添加了一个自定义字段。字段ID为tour_length
. 我使用将自定义字段值添加到循环中<?php the_field(\'tour_length\'); ?>
但是,当贴子显示在前端时,它只显示第一个项目的值,并对每个后续项目重复该值。
{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}
有人能告诉我为什么会这样吗?
EDIT: 实现Fleuv指出的解决方案解决了在搜索中显示帖子的问题。然而,类别中显示的帖子仍然显示出问题。下面是类别循环:
{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}