wsgg
wsgg

Reputation: 984

Get custom fields in custom taxonomy template

I've managed to list out all direct children of the custom parent taxonomy..

 Chocolates
 Marshmallows  
 Popcorn
 ..and so on...

with this code below.

<?php 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) { 
$terms = get_terms( 'product-type', 'parent='.$term->term_id ); } 
else { $terms = get_terms( 'product-type', 'parent='.$term->parent ); } 
foreach($terms as $term) { 
echo '
<div class="snack_type">
<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>
</div>

'; }
?>

Each of the taxonomies showing above has an image uploaded to it with custom fields (advanced custom fields). How do i show a custom field (product_type_image) in each of the div that gets generated? like this

 Chocolates [product_type_image]
 Marshmallows [product_type_image] 
 Popcorn [product_type_image]
 ..and so on...

i was trying out with

 $productimage = get_field('product_type_image', $term->taxonomy.'_'.$term->term_id);

but it was unsuccessful to try to show anything

Upvotes: 0

Views: 622

Answers (1)

user1696812
user1696812

Reputation:

add this line

echo '<img src="' . get_field('product_type_image', $term->taxonomy . '_' . $term->term_id) . '"/>';

so it should look like this

<?php 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) { 
$terms = get_terms( 'product-type', 'parent='.$term->term_id ); } 
else { $terms = get_terms( 'product-type', 'parent='.$term->parent ); } 
foreach($terms as $term) { 
echo '<div class="snack_type"><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></div>'; 
echo '<img src="' . get_field('product_type_img', $term->taxonomy . '_' . $term->term_id) . '"/>';
}
?>

Upvotes: 1

Related Questions