我试图利用ACF关系将成员关联到不同的委员会。在前端,我想知道委员会的名称以及与之相关的董事会成员。我试图遵循ACF Relationship 指南,几个堆栈答案,和其他东西,但我对WP很陌生,所以我一直没有成功。
Here are screenshots of my Wordpress setup

Here is my code:
<?php
$args = array(\'post_type\' => \'committees\');
$the_query = new WP_Query($args);
if (have_posts()):
while ($the_query->have_posts()): $the_query->the_post();
?>
<li style=\'margin-bottom: 15px;\'>
<h5 style=\'margin: 0;\'><?php echo the_field("committee_name"); ?></h5>
<ul>
<li>associated committee members repeated here</li>
</ul>
</li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
Edits
<?php
$args = array(\'post_type\' => \'committees\');
$the_query = new WP_Query($args);
if (have_posts()):
while ($the_query->have_posts()): $the_query->the_post();
$members = get_field(\'committee_members\', $member->ID);
?>
<li style=\'margin-bottom: 15px;\'>
<h5 style=\'margin: 0;\'><?php echo the_field("committee_name"); ?></h5>
<ul>
<?php foreach($members as $member)
echo "<li>";
echo $member->post_title;
echo "</li>";
?>
</ul>
</li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>


SO网友:gdaniel
关系字段可以返回对象或Post ID。这取决于您(您可以在创建字段时选择它)。无论哪种方式,该对象或ID都将在数组中返回,因为该字段为您提供了选择多个值的选项。
因此,在您的循环中:
$members = get_field(\'committee_members\'); // assuming that\'s the field slug
//if it\'s returning the object
foreach($members as $member){
echo $member->post_title; //member name
}
这将把所有委员会成员分配给$成员。然后,您只需遍历所有$成员并打印他们的姓名,或者您需要的与该帖子相关的任何其他内容。如果委员会成员还具有ACF自定义字段,则可以通过以下方式访问该字段:
get_field("YOU_FIELD_NAME", $member->ID);