Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created January 21, 2026 13:41
Show Gist options
  • Select an option

  • Save plugin-republic/b7d8d8bf04262b5fb31d3fb469c1b50a to your computer and use it in GitHub Desktop.

Select an option

Save plugin-republic/b7d8d8bf04262b5fb31d3fb469c1b50a to your computer and use it in GitHub Desktop.
<?php
/**
* If a product has an alternative featured image, display that on the archive page
*/
function pr_demo_product_get_image( $image, $product, $size, $attr, $placeholder ) {
if( is_product() ) {
// We're on a product page so just return the image
return $image;
}
$product_id = $product->get_id();
// Check if there's a custom image for the archive
$image_url = get_post_meta( $product_id, 'pr_custom_archive_image', true );
if( $image_url ) {
$attachment_id = attachment_url_to_postid( $image_url );
$image = wp_get_attachment_image( $attachment_id, 'woocommerce_thumbnail' );
}
return $image;
}
add_filter( 'woocommerce_product_get_image', 'pr_demo_product_get_image', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment