Skip to content

Instantly share code, notes, and snippets.

@askwpgirl
Last active May 17, 2021 21:01
Show Gist options
  • Save askwpgirl/702d1d6955309a47bf5d4aee3cb15cd2 to your computer and use it in GitHub Desktop.
Save askwpgirl/702d1d6955309a47bf5d4aee3cb15cd2 to your computer and use it in GitHub Desktop.
Get taxonomy ACF image field in a loop for all the taxonomy terms
<?php
// Set your ACF Image field output to array in ACF Field Group
function my_tax_grid() {
$terms = get_terms( array(
'taxonomy' => 'my_taxonomy', // Swap in your custom taxonomy name
'hide_empty' => true,
'orderby' => 'name',
'order' => 'DESC'
));
echo '<div class="tax-grid flex-grid">';
// Loop through all terms with a foreach loop
foreach( $terms as $term ) {
// Use get_term_link to get terms permalink
// USe $term->name to return term name
?>
<div class="col">
<a href="<?php echo get_term_link( $term ); ?>">
<?php $image = get_field('my_cat_image', $term );
if( $image ) {
$url = $image['url'];
$size = 'square-grid';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
?>
<img src="<?php echo esc_url($thumb); ?>" />
<?php
}
?>
<span class="term-name"><?php echo $term->name; ?></span>
</a>
</div>
<?php
}
echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment