我建议创建3个单独的[\'associated\\u items\']字段,而不是1个。
例如:
<h2>Posts related to the Company</h2>
<?php $assoc_articles = get_field(\'associated_articles\');?>
<?php if( $assoc_articles ): ?>
<div class="relatedposts">
<h3>Articles</h3>
<?php foreach( $ass_articles as $article): ?>
<?php setup_postdata($article); ?>
<?php get_template_part( \'content\', \'excerpt\' ); ?>
<?php endforeach; ?>
</div><!-- div.relatedposts -->
<?php wp_reset_postdata(); ?>
<?php $assoc_presentations = get_field(\'associated_presentations\');?>
<?php if( $assoc_presentations ): ?>
<div class="relatedposts">
<h3>Presentations</h3>
<?php foreach( $assoc_presentations as $presentation ): ?>
<?php setup_postdata($presentation ); ?>
<?php get_template_part( \'content\', \'excerpt\' ); ?>
<?php endforeach; ?>
</div><!-- div.relatedposts -->
<?php wp_reset_postdata(); ?>
<?php $assoc_news = get_field(\'associated_news\');?>
<?php if( $assoc_news ): ?>
<div class="relatedposts">
<h3>News</h3>
<?php foreach( $assoc_news as $news_item ): ?>
<?php setup_postdata($news_item ); ?>
<?php get_template_part( \'content\', \'excerpt\' ); ?>
<?php endforeach; ?>
</div><!-- div.relatedposts -->
<?php wp_reset_postdata(); ?>
我假设您使用上述代码示例是因为您喜欢它。但是,如果您只需要一个到帖子的链接,那么实际上不需要设置\\u postdata()。。。您可以简单地使用$post->ID,然后使用如下内容循环结果:
<?php foreach( $assoc_news as $news_item ):
$news_ID = $news_item->ID; ?>
<li><a href="<?php get_permalink($news_ID ); ?>"><?php get_the_title($news_ID ); ?></a></li>... etc
我希望这有帮助。