0

I'm trying to get an individual posts custom taxonomy ID to then print an image with ACF. I've looked at the documentation here on ACF and this support ticket too.

From my understanding it looks like it should work if if I use get_queried_object(), however I'm not having much luck.

Here is my code:

<?php $producers_assessment_object = get_queried_object();?>
<?php the_field('badge', 'producers_assessment_' . $producers_assessment_object->term_id); ?>

If I manually put in the post ID like so <?php the_field('badge', 'producers_assessment_7'); ?> the field works as I would like it to.

I've tried using this code below from another SO questions, just to see if I can the term Id in anyway and this also didn't work.

$cate = get_queried_object();
$cateID = $cate->term_id;
echo $cateID;

Here is the code from my functions.php for my custom taxonomy:

register_taxonomy(  
        'producers_assessment',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        'producers',        //post type name
        array(  
            'hierarchical' => true,  
            'label' => 'Assessment Stage',  //Display name
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'producer-assessment', // This controls the base slug that will display before each term
                'with_front' => false // Don't display the category base before 
            )
        )  
    ); 
4
  • 1
    I think maybe you're confusing the global queried object present on a term archive page with the individual terms assigned to each post. Is this a taxonomy term archive page? That's the only case where the term data would be the queried object. You say each post so it sounds like you want something like get_the_terms instead to fetch the terms assigned to each post.
    – Milo
    Mar 5, 2018 at 16:52
  • I haven't used ACF in a long while but term_id is not a property of the post object. Take a look at get_the_terms() instead.
    – jdm2112
    Mar 5, 2018 at 16:54
  • Ha, or what Milo just said! Always listen to Milo.
    – jdm2112
    Mar 5, 2018 at 16:55
  • Thanks, I'll look at get_the_terms. I've updated my question to include my custom taxonomy code if this helps. Mar 5, 2018 at 16:57

1 Answer 1

1

From the help of the comments above I needed to use get_term_terms to print the ID.

I used the code below to achieve what I needed:

<?php 
    $terms = get_the_terms( $post->ID, 'producers_assessment' ); 
        foreach($terms as $term) {?>
            <?php 
            $image = get_field('badge', 'producers_assessment_' . $term->term_id);
            if( !empty($image) ): ?>
                <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" width="50" height="50" />
            <?php endif; ?>
        <?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.