I am using ACF relationship field on a Wordpress site so that the admin user can easily determine which posts are visible on that page. I have a custom taxonomy where I need to be able to get_the_terms and print the term for each post. This is normaly achieved with an foreach as mentioned in the codex.
However Im using the foreach to get the $posts so I'm not sure how to use this to also print the term name in my H3 and the term slug in the main <div>
Code below:
<?php
$posts = get_field('team_members',12);
$terms = get_the_terms( $post->ID , 'position' );
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT)
setup_postdata($post); ?>
<div class="col-4 col <?php echo $term->slug;?>">
<article id="post-<?php the_ID(); ?>" <?php post_class('team-item'); ?>>
<hgroup>
<?php the_title( sprintf( '<h2 class="alt-heading-4">', esc_url( get_permalink() ) ), '</h2>' ); ?>
<h3><?php echo $term->name;?></h3>
</hgroup>
<div class="team-entry-content">
<?php the_content();?>
</div><!-- .entry-content -->
<div id="team-shadow"></div>
</article><!-- #post-## -->
</div>
<?php endforeach; ?>
<?php wp_reset_postdata();?>
<?php endif; ?>
$post->IDused in theget_the_terms()call relevant to the$postdefined in iteration offoreach ($posts as $post), or is that$postvariable from earlier code? In other words, are you retrieving terms for each postIDlooped fromget_field()? - Michael Berkowskiforeach ($posts as $post)is anotherforeach ($terms as $term)nested within it, wherein you move the call toget_the_terms()to happen inside the $postsforeachloop rather than before it. But as I said, I'm unfamiliar with WP.... - Michael Berkowski$posts = get_field()...foreach($posts as $post) { $terms = get_the_term()... foreach ($terms as $term) { //print term info } }- Michael Berkowski