22

I am using the below code to try and retrieve an image field called 'image' from a taxonomy term using the Advanced Custom Fields plugin. This code is based on the documentation on the ACF website here.

It should be noted this code is being used within the taxonomy.php template, and I am unable to specify particular taxonomy and/or terms as I need the code to detect the current taxonomy and term, based on the page the user and clicked through to.

Any help much appreciated!

<?php get_header(); ?>
<?php get_sidebar(); ?>

<section id="hero-image">
    <div class="gradient-overlay">
        <?php 
        // vars
        $queried_object = get_queried_object(); 
        $taxonomy = $queried_object->taxonomy;
        $term_id = $queried_object->term_id;  

        // load thumbnail for this taxonomy term (term object)
        $image = get_field('image', $queried_object);

        // load thumbnail for this taxonomy term (term string)
        $image = get_field('image', $taxonomy . '_' . $term_id);
        ?>
    </div>
    <div class="grid">
        <header class="unit full-width">
            <a href="<?php echo home_url(); ?>/" title="Kurdistan Memory Programme" class="logo"><?php bloginfo( 'name' ); ?></a>
        </header>
        <footer class="unit one-half">
            <h1><?php single_cat_title(); ?></h1>
            <h4 class="scroll-down">Scroll down to continue</h4>
        </footer>
    </div>
</section>

<?php get_footer(); ?>

enter image description here

dungey_140
  • 2,602
  • 7
  • 34
  • 68
  • Did you set the ACF field to the taxonomy appropriately and are you calling the correct field name in your code? You've supplied the code from the example in ACF's documentation, but none of your production code. If possible, provide a screen shot of your ACF settings for this taxonomy field. – Plummer Dec 10 '15 at 16:31
  • Your second $image "$image = get_field('image', $taxonomy . '_' . $term_id);" should be working to grab the value of a taxonomy term's custom field. What is it currently returning? What is your return value set to for the custom field? – Joe Dec 10 '15 at 16:31
  • Hi @Plummer, i've updated my question to include the full code from the taxonomy.php file and a screenshot of the ACF settings – dungey_140 Dec 10 '15 at 16:56
  • @Joe - currently nothing is being returned from the ACF code – dungey_140 Dec 10 '15 at 16:58
  • With those field settings, an object should be returned. So does a var_dump($image) return nothing as well? – Joe Dec 10 '15 at 17:00
  • @dungey_140: what does the location value look like for the Field Group? http://i.imgur.com/ZPjSbBc.png – Plummer Dec 10 '15 at 17:04
  • @Plummer - Exactly the same as your example, but instead of 'Category' it is set to 'All' so as to apply to every Taxonomy. I have set an image for the page I am working on, which is not returning an image. – dungey_140 Dec 10 '15 at 17:06
  • @Joe - Afraid I'm not sure how to do that, could you elaborate? – dungey_140 Dec 10 '15 at 17:07
  • @dungey_140: you set an image to the page, or you set an image to the taxonomy? If it's taxonomy, you need to go into Post > Category and set the image. http://i.imgur.com/LzNpXjS.png – Plummer Dec 10 '15 at 17:10
  • below where you define $image, add the following php code: var_dump($image); – Joe Dec 10 '15 at 17:10
  • Check this link: http://support.advancedcustomfields.com/forums/topic/displaying-multiple-values-from-custom-taxonomy-terms/ – Bipbip Dec 10 '15 at 17:14
  • @Plummer Yes I have set an image for the taxonomy like this – dungey_140 Dec 10 '15 at 17:23
  • @Joe - I got the output, have added another image to the question... – dungey_140 Dec 10 '15 at 17:26
  • Just added an answer. You are obtaining the data you were expecting, which is the photo object, but then you need ot set the output to a useful format. – Joe Dec 10 '15 at 17:30
  • @Joe - Brilliant! Thank you so much for your help! I've accepted your answer :) Along a similar vein, might you be able to shed any light on the following question? http://stackoverflow.com/questions/34159550/wp-query-taxonomy-php-to-display-posts-from-current-term?noredirect=1#comment56101232_34159550 – dungey_140 Dec 10 '15 at 17:43

3 Answers3

24

Ok, so you are getting the value of the field, you just need to set how it should output, like so:

$image = get_field('image', $taxonomy . '_' . $term_id);
echo '<img src="'.$image['sizes']['thumbnail'].'" alt="$image['alt']" />';

This assumes that you want to use the thumbnail image size. If using a different size, change that text to the appropriate image size.

If you want to return the full size image, use the following code:

$image = get_field('image', $taxonomy . '_' . $term_id);
echo '<img src="'.$image['url'].'" alt="$image['alt']" />';
Joe
  • 1,792
  • 1
  • 16
  • 25
  • Hi @Joe - I'm trying to change from 'thumbnail' to 'full' for the image size, but when I do the code fails to work. It does work with the 'large' and 'medium' settings however. Any ideas? – dungey_140 Dec 10 '15 at 18:09
  • Change the code to this: echo ''; that should return the full size URL. – Joe Dec 10 '15 at 18:11
  • This is confusing since you didn't explain that $taxonomy is the term object obtained using get_terms(). – Adal Dec 30 '19 at 04:42
  • I didn't feel it was necessary to copy/paste all of the original questioner's code, I just included additional code to give the desired output. – Joe Dec 31 '19 at 14:07
  • the example I overlook from [Get a value from different objects](https://www.advancedcustomfields.com/resources/get_field/) `$post_id = "category_3";` – Hebe Feb 03 '22 at 18:15
0
  <?php 
                                $terms = get_field('best_seller_laptops_pc_category');
                                if( $terms ): ?>
                                <?php foreach( $terms as $term ):
                                    $thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
                                    $term_img = wp_get_attachment_url(  $thumb_id );
                                ?>


           <div class="col-lg-6">
                <div class="addbox1">
                    <img alt="" src="<?php echo $term_img;?>">
                    <div class="contain">
                        <h3>
                           <?php echo esc_html( $term->name ); ?>
                        </h3>
                        <h4><?php echo esc_html( $term->description ); ?></h4>
                        <a href="<?php echo get_term_link( $term ); ?>">LEARN MORE</a>
                        <a href="<?php echo get_term_link( $term ); ?>" class="btn">buy now</a>
                    </div>
                </div>
            </div>

        <?php endforeach; ?>
        <?php endif; ?> 
Purnendu Sarkar
  • 332
  • 2
  • 12
-5

taxonomy.php is a template for a custom taxonomy based on the same model of archives.php In this kind of template, you should use the wordpress loop

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Them, you can pass in get_field, the post ID like that as 2nd parameter:

$image = get_field('image', $post->ID);
Bipbip
  • 184
  • 8
  • 1
    Check the source reference from ACF. They're setting a field TO a taxonomy and then trying to retrieve that value. Also, on a personal note, I did not downvote your answer, someone else did, as this is what I thought OP was asking for at first as well. – Plummer Dec 10 '15 at 16:29
  • $image = get_field('image', $queried_object); should return an array. make a var_dump of it – Bipbip Dec 10 '15 at 16:37