I would like to change the URL of an image from wp_get_attachment_image_src for Product Posts based on their Category, for example:

If a product post is in product category 1, this will be it's image source 'http://newimagesrc.com/myimage_category_1.jpg' and if it is in product category 2 this will be it's image source 'http://newimagesrc.com/myimage_category_2.jpg' and so on...

Below is a working code but it has no feature that I needed to assign different image per category:

    function alterImageSRC($image, $attachment_id, $size, $icon){        
    $image[0] = 'http://newimagesrc.com/myimage.jpg';

    return $image;

    add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);
  • Hope someone can help, thanks in advance. – BlogMarkz Apr 11 at 15:11
  • If I understood right, you want to show different product images depending on product category. Guess it's a bad idea to do it like this, especially with hardcoded urls. But if you want to show it on product/post page, you will need a post id, then check what categories has this post and just then do an if statement. Did you tried something like this? – anton Apr 22 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.