Print image title on woocommerce product featured image

woo product image title

The above code gets the image title and prints it just under the main Woocommerce image. To add a title to the image, you need to go to Media -> Library and add your text to your image of choice.

function gcw_insert_captions( $html, $attachment_id ) {
	$captions = '';
	$title = get_post_field( 'post_title', $attachment_id );
	if( !empty( $title ) ) {
		$captions .= '<h2><br>' . esc_html( $title ) . '</h2>';
	}
	$description = get_post_field( 'post_excerpt', $attachment_id );
	if( !empty( $description ) ) {
		$captions .= '<p>' . $description . '</p>';
	}
	if( !empty( $captions ) ) {
		$captions = '<div class="gcw-caption">' . $captions . '</div>';
		
		$html = preg_replace('~<\/div>$~', $captions . '</div>', $html );
	}
	return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'gcw_insert_captions', 10, 2 );

Leave a Comment