Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image from taxonomy term using ACF

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

like image 368
dungey_140 Avatar asked Dec 10 '15 16:12

dungey_140


People also ask

How do I display an ACF image in WordPress?

Link to heading#Editing fieldsNavigating to the Media > Library admin page and selecting an attachment. Using the Add Media button when editing a post's content. Using an ACF image field or file field to select an attachment.

How do I find taxonomy images in WordPress?

Go to your WP-admin ->Settings menu a new “Taxonomy Image” page is created. Go to your WP-admin ->Settings ->Taxonomy Image displayed in the taxonomies list form where you can select the taxonomies you want to include it in WP Custom Taxonomy Image.

How do I get the category custom field value in WordPress?

php $categories = get_categories( array( 'orderby' => 'name', 'order' => 'ASC' ) ); foreach( $categories as $category ) { if($category->name != "Uncategorized") { $cat_title = get_term_meta( $category->term_id, '_pagetitle', true ); echo ' <div class="col-md-4"><a href="' . get_category_link($category->term_id) .


1 Answers

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']" />';
like image 188
Joe Avatar answered Sep 19 '22 18:09

Joe