Specifying a size when displaying an image associated with a taxonomy through ACF

I created a “books” custom taxonomy for my “book” CPT. Using ACF, I assigned an image to each my custom taxonomy terms.

I have a page where I display all my “books” terms as images. It works but now I want to specify a particular size to these images. The code of the page:

Read More
<?php
$taxonomy = 'books';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_terms) {
    $taxonomy_image = get_field( 'taxonomy_image', 'books_'.$tax_terms->term_id );
    $taxonomy_image_url = $taxonomy_image['url'];               
    ?>
    <img src="<?php echo $taxonomy_image_url; ?>" alt="" />
    <?php
}
?>
</ul>

I think the ACF doc page about images contains the key to what I’m trying to do – especially the part about image size, but I can’t seem to find a way to integrate it to my code successfully. Any suggestion?

Related posts

1 comment

  1. It appears you currently have the field set to return the image object, if you scroll down to the bottom of the page you linked you’ll see an example object:

    Array
    (
        [id] => 540
        [alt] => A Movie
        [title] => Movie Poster: UP
        [caption] => sweet image
        [description] => a man and a baloon
        [url] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
        [sizes] => Array
            (
                [thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-150x150.jpg
                [medium] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-300x119.jpg
                [large] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                [post-thumbnail] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                [large-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up.jpg
                [small-feature] => http://localhost:8888/acf/wp-content/uploads/2012/05/up-500x199.jpg
            )
    
    )
    

    so for a specific size you want to replace $taxonomy_image['url'] with $taxonomy_image['sizes']['thumbnail'], where thumbnail is the desired size key.

Comments are closed.