我不確定我的語法是否有錯誤,但由于某種原因,如果我插入以下 PHP 代碼,則會導致其下方的 ACF 欄位<?php the_field('fieldname'); ?>完全從內容中消失(就像甚至沒有出現在 html 代碼中一樣) . 如果我將它粘貼在 ACF 欄位下方,那很好,只有當它位于上方時才會出現此問題。
<?php
$args = array( 'post_type' => 'post', 'numberposts' => -1);
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ): setup_postdata( $post );
?>
<div class="col">
<p class="ingress">“<?php the_field('review'); ?>”</p>
<img src="<?php the_field('star_rating'); ?>" class="star-rating"><br>
<img src="<?php the_field('image'); ?>" class="avatar">
<p class="name-location"><?php the_field('name_location'); ?></p>
</div>
<?php endforeach; ?>
uj5u.com熱心網友回復:
setup_postdata()修改$post. 當您運行時,這會將帖子設定為回圈中的不同帖子setup_postdata()。在你之后endforeach;,你應該wp_reset_postdata();將全域變數放回它們在你的 foreach 回圈之前的位置。
有關 setup_postdata 的更多資訊,請參閱檔案
這是他們對所發生事情的描述,可能比我的措辭更好。
setup_postdata() 不會分配全域 $post 變數,因此您自己執行此操作很重要。如果不這樣做,將導致使用上述任何全域變數和 $post 全域變數的任何鉤子出現問題,因為它們將參考單獨的物體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533061.html
標籤:php高级自定义字段
