So, I have created a custom field inside my product categories for an image to be uploaded (I am using both the standard WP category image, as well as this second one). I need to display the field inside a loop, but am having trouble getting it to show.
<div class="product-categories-list">
<?php foreach( $prod_categories as $prod_cat ) {
$cat_thumb_id = get_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
$shop_catalog_img = wp_get_attachment_image_src( $cat_thumb_id, 'large' );
$term_link = get_term_link( $prod_cat, 'product_cat' );
$cat_title = $prod_cat->name;
// THIS FIELD
$cat_term = get_queried_object();
$hover_img = get_field('hover_image', $cat_term);
?>
<div class="product-category">
<a href="<?php echo $term_link; ?>">
<div class="image">
<img class="base-image" src="<?php echo $shop_catalog_img[0]; ?>" alt="<?php echo $cat_title; ?>" />
<img class="hover-image" src="<?php echo $hover_img['url']; ?>" alt="<?php echo $cat_title; ?>">
</div>
<h3><?php echo $cat_title; ?></h3>
</a>
</div>
<?php } wp_reset_query();?>
</div>
What is this returning? Is it what you think it should be?
$cat_term = get_queried_object();
Ah, that just makes the site have a critical error. I copied that solution from my ‘product tag archive’ pages – it works on there and displays a custom image field. I’m assuming this is because I’m not on an actual archive page, as the code in question is from a flexible content block that will be used on multiple pages.
Remove that line and use
$hover_img = get_field('hover_image', $prod_cat );
Oh wow, so simple…
Works like a charm, thanks a lot!