0
votes

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; ?>
1
So I don't need to read extensive documentation, is the $post->ID used in the get_the_terms() call relevant to the $post defined in iteration of foreach ($posts as $post), or is that $post variable from earlier code? In other words, are you retrieving terms for each post ID looped from get_field()? - Michael Berkowski
$post->ID isn't used in get_the_terms. Everything would stay as is, except the foreach would look like 'foreach( $terms as $term)' - probablybest
Due to unfamiliarity with WP, I am not fully certain of the intent, but it appears to me that what you need to do inside the outer foreach ($posts as $post)is another foreach ($terms as $term) nested within it, wherein you move the call to get_the_terms() to happen inside the $posts foreach loop rather than before it. But as I said, I'm unfamiliar with WP.... - Michael Berkowski
The algorithm I mean is: $posts = get_field()...foreach($posts as $post) { $terms = get_the_term()... foreach ($terms as $term) { //print term info } } - Michael Berkowski

1 Answers

2
votes

As the terms are connected to your post , you have to place the :

$terms = get_the_terms( $post->ID , 'position' );

inside the foreach loop , outside that it will not work at all , as the $post->ID will be error:

trying to get the property of non object

so the solution is take the $terms = get_the_terms( $post->ID , 'position' ); and add it inside the foreach loop:

<?php
$posts = get_field('team_members',12);
if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) 
    setup_postdata($post);
       $terms = get_the_terms( $post->ID , 'position' ); ?>
    <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>' ); ?>
              <?php foreach($terms as $term) {?>
                <h3><?php echo $term->name;?></h3>
              <?php } ?>
            </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; ?>

i hope it did help :).