Last active
May 17, 2021 21:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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