Published in WordPress
avatar
1 minute read

Get image from taxonomy term using ACF

I'm utilizing the beneath code to attempt to recover a picture field called 'picture' from a scientific classification term utilizing the High level Custom Fields module. This code depends on the documentation on the ACF site here.

It ought to be noticed this code is being utilized inside the taxonomy.php format, and I can't determine specific scientific classification and additionally terms as I want the code to recognize the ongoing scientific classification and term, in view of the page the client and navigated to.

Any assistance 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(); ?>

Solutions

Alright, so you are getting the worth of the field, you simply have to set how it ought to yield, as so:

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

This expects that you need to utilize the thumbnail picture size. If utilizing an alternate size, change that text to the fitting picture size.

To return the standard picture, utilize the accompanying code:

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

Comments