Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display Advanced Custom Field Thumbnail on Taxonomy Archive WordPress

  • SOLVED

Hi all,

I have added a custom field via the Advanced Custom Field plug-in to my taxonomy of 'Make & Model' and wish to display the thumbnail for each term via the taxonomy-make_model template. However the code supplied via the ACF documentation requires the taxonomy term ID to be called. Obviously as I wish for the code to work for all the taxonomy terms I need this ID to be populated dynamically.

Here is the link to the ACF documentation detailing how to display taxonomy meta data:
http://www.advancedcustomfields.com/docs/tutorials/retrieving-values-from-other-pages-taxonomy-user-media/

So far I've tried both the code snippets below to no avail. Can anyone supply me with the correct code please?

<?php
$terms = get_query_var('term_id');
$attachment_id = get_field('thumbnail', 'make_model_$terms');
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$thumb = wp_get_attachment_image_src( $attachment_id, $size );
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>


<?php
$terms = get_the_terms( $post->ID , 'make_model');
$attachment_id = get_field('thumbnail', 'make_model_$terms');
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$thumb = wp_get_attachment_image_src( $attachment_id, $size );
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>


The custom field has the field name of 'thumbnail'.

Many thanks,

Adam.

Answers (3)

2012-09-24

Michael Caputo answers:

what happens when you do this:


echo $attachment_id;


flint_and_tinder comments:

For both sets of code, nothing but empty img tag. e.g.
<img src>

The same as before. I'm not sure either code example calls or executes the taxonomy ID correctly.


Michael Caputo comments:

I think I see the problem, you're calling "'make_model_$terms'" which doesn't make sense because a variable starts with $

maybe change to:
$attachment_id = get_field('thumbnail', '$terms');
or


Michael Caputo comments:

oh i think i see what you're trying to do.
maybe you can construct your variable like this:

<?php

$terms = get_the_terms( $post->ID , 'make_model');

$variable = 'make_model_'. $terms;

$attachment_id = get_field('thumbnail', $variable);

$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)

$thumb = wp_get_attachment_image_src( $attachment_id, $size );

?>

<p><img src="<?php echo $thumb[0]; ?>" /></p>


flint_and_tinder comments:

Still not working I'm afraid. Here's the whole page code (so far) if that helps;

<?php

get_header(); ?>

<div class="clearfix">

<div class="nine-column left">

<section>

<h1 class="entry-title"><?php single_term_title(); ?> </h1>

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<?php
$queried_object = $wp_query->get_queried_object();

if (isset($queried_object->term_id)) {
$terms = get_terms('make_model', 'child_of=' . $queried_object->term_id);
} else {
$terms = get_terms('make_model', 'parent=0');
}

if (!$terms && isset($queried_object->term_id)) {
/* there are no child terms anymore, so this must be a model (audi a1) */
$terms = array($queried_object);
}

?>
<?php
if ($terms) :
foreach ($terms as $term) :
?>

<header class="shape-header">
<h4><a href="<?php print get_term_link($term); ?>"><?php echo $term->name; ?></a></h4>
</header>

<table class="car-table">
<tr>
<th>Model</th>
<th>Fuel</th>
<th>Type</th>
<th>RRP</th>
<th>Saving</th>
<th>CO<sup>2</sup></th>
<th class="price-col">Price</th>
</tr>
<?php $loop = new WP_Query(array('make_model' => $term->slug));
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<tr>
<td><a href="<?php the_permalink(); ?>"><?php the_title();?></a></td>
<td><?php the_field('fuel');?></td>
<td><?php the_field('transmission');?></td>
<td><?php the_field('rrp');?></td>
<td><?php the_field('save');?></td>
<td><?php the_field('co2');?> g/km</td>
<td><a href="index.php?p=226" class="list-quote-link">Get a quote</a></td>
</tr>
<?php endwhile; ?>
</table>


<?php
endforeach;
endif;
?>
</div><!-- #post-## -->

</section><!-- #primary -->

<section>

<?php
$termDiscription = term_description( '', get_query_var( 'make_model' ) );
if($termDiscription != '') : ?>
<div class="tag-desc"><?php echo $termDiscription; ?></div>
<?php endif; ?>

</section>

</div>
<div class="three-column right">

<?php



$terms = get_the_terms( $post->ID , 'make_model');



$variable = 'make_model_'. $terms;



$attachment_id = get_field('thumbnail', $variable);



$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)



$thumb = wp_get_attachment_image_src( $attachment_id, $size );



?>



<p><img src="<?php echo $thumb[0]; ?>" /></p>

<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>


And a link to the page in question:

http://www.flintandtinder-proofing.co.uk/my-car-broker/new-cars/audi/a1/


Michael Caputo comments:

what do you get when you echo all the variables here:

echo $terms;
echo $variable;
echo $attachement_id;
echo $size;
echo $thumb;


You should be able to see where it's going wrong pretty easily.


Michael Caputo comments:

Slick looking site btw!


flint_and_tinder comments:

I'm pretty poor at php I'm afraid, so it will mean nothing to me. If I remove your previous code and replace it with the code you've suggested above, it just displays the word 'Array'.

http://www.flintandtinder-proofing.co.uk/my-car-broker/new-cars/audi/a1/


Michael Caputo comments:

Ok, we need to know which variable is outputting array.


<h1>Terms</h1>
echo $terms;
<h1>variable</h1>
echo $variable;
<h1>attachement_id</h1>
echo $attachement_id;
<h1>size</h1>
echo $size;
<h1>thumb</h1>
echo $thumb;


Michael Caputo comments:

Keep in mind that has to go after all your variables are set.


flint_and_tinder comments:

With your previously suggested code before it, like so:


<?php
$terms = get_the_terms( $post->ID , 'make_model');
$variable = 'make_model_'. $terms;
$attachment_id = get_field('thumbnail', $variable);
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$thumb = wp_get_attachment_image_src( $attachment_id, $size );
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>
<h1>Terms</h1>
<?php echo $terms; ?>
<h1>variable</h1>
<?php echo $variable; ?>
<h1>attachment_id</h1>
<?php echo $attachment_id; ?>
<h1>size</h1>
<?php echo $size;?>
<h1>thumb</h1>
<?php echo $thumb; ?>
<?php get_sidebar(); ?>


I get this:
http://www.flintandtinder-proofing.co.uk/my-car-broker/new-cars/audi/a1/

Thanks for the compliment re: the design. I'm a designer rather than a developer by trade really. Thanks for the effort so far as well. I didn't envision this being so complicated.


Michael Caputo comments:

How about this:

<?php

$terms = get_the_terms( $post->ID , 'make_model');

$variable = 'make_model_'. $terms;

$attachment_id = get_field('thumbnail', $variable);

$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)

$thumb = wp_get_attachment_image_src( $attachment_id, $size );

?>

<p><img src="<?php echo $thumb[0]; ?>" /></p>

<h1>Terms</h1>

<?php echo $terms; ?>

<h1>variable</h1>

<?php echo $variable; ?>

<h1>attachment_id</h1>

<?php echo $attachment_id; ?>

<h1>size</h1>

<?php echo $size;?>

<h1>thumb</h1>

<?php echo $thumb; ?>

<h1>print_r terms</h1>
<?php print_r($terms); ?>

<h1>print_r variable</h1>
<?php print_r($variable); ?>


<?php get_sidebar(); ?>


flint_and_tinder comments:

http://www.flintandtinder-proofing.co.uk/my-car-broker/new-cars/audi/a1/


flint_and_tinder comments:

Hi Michael,

Thanks for all your help but Max's code below is what got it fixed.

Adam.

2012-09-24

daas answers:

Try to use:

$attachment_id = get_field('thumbnail', 'make_model_' . $terms);


flint_and_tinder comments:

Still no joy. :-(


daas comments:

Hm... What is the result of

var_dump($terms);


daas comments:

I'm not quiet sure about this code

$attachment_id = get_field( 'thumbnail', 'make_model_' . $queried_object->term_id );
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$thumb = wp_get_attachment_image_src( $attachment_id, $size );
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>

get_the_terms outputs array, but we need just ID.


flint_and_tinder comments:

array(1) { [30]=> object(stdClass)#80 (10) { ["term_id"]=> string(2) "30" ["name"]=> string(2) "A1" ["slug"]=> string(2) "a1" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(2) "40" ["taxonomy"]=> string(10) "make_model" ["description"]=> string(503)


daas comments:


<?php
$terms = get_the_terms( $post->ID , 'make_model');
$attachment_id = get_field('thumbnail', 'make_model_' . $terms->term_id);
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$thumb = wp_get_attachment_image_src( $attachment_id, $size );
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>


flint_and_tinder comments:

Still get a blank image tag I'm afraid.


daas comments:

I made a mistake. What about the previous one?
var_dump($queried_object->term_id); should output term ID ( in this case 30 )


flint_and_tinder comments:

I've confused about what code I should be leaving in now.

If I just add

<?php
var_dump($queried_object->term_id);
?>


and nothing else, I get this:

string(2) "30"


daas comments:

Oh, By previous one i mean:

$attachment_id = get_field( 'thumbnail', 'make_model_' . $queried_object->term_id );
$size = "thumbnail"; // (thumbnail, medium, large, full or custom size)
$thumb = wp_get_attachment_image_src( $attachment_id, $size );
?>
<p><img src="<?php echo $thumb[0]; ?>" /></p>


As you said $queried_object->term_id outputs 30 (ID of term). So it looks correct ( based on tutorial you have provided )


flint_and_tinder comments:

Still get an empty img tag.

Considering giving up.


flint_and_tinder comments:

Haha! I take that back. It works! I am so sorry, but I realised I'd set the custom field type slightly wrong. I was pulling in the thumbnail object rather than the image ID. With that changed your code above works.

Thank you!


daas comments:

Hmm. We can also check does $attachment_id variable outputs attachment id in fact.

$attachment_id = get_field( 'thumbnail', 'make_model_' . $queried_object->term_id );
var_dump($attachment_id);

2012-09-24

Arnav Joy answers:

when you add image then can you see it at admin panel in the taxonomy edit page?


flint_and_tinder comments:

Yes I can: