Using Custom Taxonomy Term ID for Custom Field Using Custom Taxonomy Term ID for Custom Field wordpress wordpress

Using Custom Taxonomy Term ID for Custom Field


Try this code this should work for you

 global $post;    // load all 'sermon_speaker' terms for the post    $terms = get_the_terms($post->ID, 'sermon_speaker');    // we will use the first term to load ACF data from    if( !empty($terms) )    {        $term = array_pop($terms);        $custom_field = get_field('sermon_speaker_image', $term );        // do something with $custom_field        //i.e. echo $custom_field;        //i.e. echo "<img src='$custom_field'/>";       ?><div class="sermon-speaker-image" style="background-image: url('<?php echo $custom_field; ?>');"></div><?    }

You can read more on the official documentation of Advanced custom fields


Two thoughts:

First One

<div class="sermon-speaker-image" style="background-image: url('<?php the_field( 'sermon_speaker_image', 'sermon_speaker_' ?> . <?php echo $term_id ?> ); ?>');"></div>

I use echo to actually display it.

Second One

This is my favorite and actually working one.You can use jQuery to set the attribute background-image where you want and append the value from the custom type.

$(document).ready(function() {     var new_link = $(".sermon-speaker-image").attr("background-image");        new_link = x.append('<?php echo $term_id; ?>','');     jQuery(".sermon-speaker-image").attr("background-image", new_link);}

Ofcourse you have to check that the (url=url+id) image actually exists.