Xxx red Xxx red - 9 days ago 7
PHP Question

Wordpress / Woocommerce Get single product photo by ID

I am trying to build custom shortcodes for wordpress / WooCommerce, so that I can select just the image or just the price of one single product, because I want to style the product page, but I can't do that with the shortcodes that made by themself...

I have looked alot on google, tryed multiple codes en previeuws but no succes :(

Maybe someone who can help my out??

This is what I have now and this works so far!!

//Select just one img from a WooCommerce product.
function singleProduct_img( $atts ) {

$a = shortcode_atts( array(
'product_id' => '',
), $atts );

return $a['product_id'];
}
add_shortcode( 'singleProduct_img', 'singleProduct_img' );


The shortcode I am using is:
[singleProduct_img product_id="233"]
and the output is "233"

Now i want that i can return the image of the product with id="233"

BTW:

Woo = version 2.6.8

WP = version 4.6.1–nl_NL

Any help wil be appreciated,

Thanks

Answer

Use get_the_post_thumbnail():

return get_the_post_thumbnail( $a['product_id'], 'medium' );
Comments