1

I have a review site, which uses a custom taxonomy called 'show' to organize posts. I have used Advanced Custom Fields to add a field to the 'show' taxonomy called 'twitter_handle'. I am trying to display the custom field 'twitter_handle' from the custom taxonomy 'show' in a single post view.

Let's say the show was 'MASH', and I made a post reviewing episode 1, and in the post, want to display the twitter handle for the MASH account. I need to query what taxonomy it is in ('show'), the taxonomy value ('mash'), and the value of 'twitter_handle' for show 'mash' in taxonomy 'show'.

The code I have now is:

<?php

// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'show');


// we will use the first term to load ACF data from
if( !empty($terms) ) {

    $term = array_pop($terms);

    $custom_field = get_field('twitter_handle', $term );

}

?>

<p><?php echo $custom_field; ?></p>

For what it's worth, I also fetch the description of each show (successfully) using the following:

<div class="show-description">
    <?php
    $posttags = get_the_terms( $post->ID, 'show');
    if ($posttags) {
      foreach($posttags as $tag) {
        echo $tag->description; 
      }
    }
    ?>
</div>

Thanks for your help.


Where I ended up:

<?php
   $posttags = get_the_terms( $post->ID, 'show');
      if ($posttags) {
         foreach($posttags as $tag) {
            $custom_field = get_field('twitter_handle', $tag );
            echo $custom_field;
  }
}
?>

Thanks to Mike for the help.

  • Your code works if the post only has one term in the show taxonomy. This grabs the first term. If you have others, it could be using them and they probably don't have a twitter_handle field. – WordPress Mike Sep 24 '15 at 0:30
  • That's interesting--I'm still having trouble getting anything to display for the twitter handle, for one (or multiple) show(s). Thanks for the help. – Eric McDaniel Sep 24 '15 at 14:54
  • Have you tried to var_dump($terms) to see whats in there? – WordPress Mike Sep 24 '15 at 15:29
  • @Mike, thanks for all the help. I edited to code I ended up using above. The 'var_dump($terms)` showed I somehow wasn't pulling any data. I don't know why I didn't think of it earlier, but I just modified the code I was using to pull the show description. – Eric McDaniel Sep 26 '15 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.