
Hey there,
I am registering the below shortcode to generate a list of categories on any page. The categories have an ACF field for the image.
function categories_display() {
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'show_count' => 1,
) );
ob_start();
?>
<div class="container">
<div class="row border rounded p-2 mb-4">
<?php foreach ($categories as $category ) :
// get the current taxonomy term
$term = get_queried_object();
// vars
$image = get_field('image', $term);
$category_link = sprintf(
'<a href="%1$s" alt="%2$s">%3$s</a>',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
esc_html( $category->name )
);
?>
<div class="col-xs-12 col-sm-6 col-md-4 p-4 p-md-1 p-xl-1 text-center d-flex justify-content-between flex-column">
<div class="cat-child-name"><p><?=$category_link?></p></div>
<div class="cat-child-image"><a href="<?php echo get_category_link( $category->term_id ); ?>"><img class="img-fluid" src="<?php echo $image['url']; ?>" /></a></div>
</div>
<?php endforeach;?>
</div>
</div>
<?php $content = ob_get_clean();
return $content;
}
add_shortcode( 'wp_categories_display', 'categories_display' );
The image isn’t loading at all. It just returns nothing.
What am I doing wrong?
Thanks
Chris
Never mind, found the issue. I had to set the variable this way:
$image = get_field('category_image', $category);
Hope this helps anyone else.
Thanks
Chris