Using Custom Taxonomy Term ID for Custom Field
Asked Answered
H

2

7

I have created a custom single-sermons.php template file for my sermons custom post type and want to include the sermon speaker image custom field of the sermon speaker for this post.

Custom Taxonomy ID: sermon_speaker Custom Field ID: sermon_speaker_image

I have been able to get the taxonomy term ID to display as a number on the page using:

<?php

$terms = wp_get_post_terms($post->ID, "sermon_speaker");
foreach ($terms as $termid) {  
echo $termid->term_id;
} 

?>

I'm trying to figure out how to use this term ID in the code below where I currently have . $term_id so that it adds the term ID to the end of the background image URL.

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

Update: The following code is a working solution based on the answer below:

<?php
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 );

}
?>
<div class="sermon-speaker-image" style="background-image: url('<?php echo $custom_field; ?>');"></div>
Haggai answered 24/12, 2014 at 11:29 Comment(0)
S
3

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

Shively answered 24/12, 2014 at 12:15 Comment(4)
Thanks for the response. I'm still not sure what to enter in the '// do something with $custom_field' area though in order to display the sermon speaker image?Haggai
Check the changes i have made to the code; it should be ready to copy paste for you. good luck. and if you didn't understand this part i would recommend watching some php beginner tutorials :)Shively
Thanks. That has worked. The only slight tweak is that I've put a ; after echo $custom_field before the closing ?>Haggai
ty for feedback i changed my answer accordingly for future reference. Best wishes to you and everyone on stackoverflow.Shively
R
0

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.

Revel answered 24/12, 2014 at 12:35 Comment(1)
i am sorry but the "First one" is not correct. the php syntax from @Steve R is the correct one. also why use jquery for this? i don't see the advantage <-- sorry for misspelled wordsShively

© 2022 - 2024 — McMap. All rights reserved.