最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Hook for single product thumbnail

programmeradmin0浏览0评论

Is there any hook to change the single product thumbnail? I did search a lot on SO as well as over the internet but no luck.

With 'thumbnail' I don't mean changing the size of the current image but I want to completely change/replace the product image (thumbnail) with a new image based on some scenario.

public function pn_change_product_image_link( $html, $post_id ){

    $url =  get_post_meta( $post_id );
    $alt = get_post_field( 'post_title', $post_id ) . ' ' .  __( 'thumbnail', 'txtdomain' );
    $attr = array( 'alt' => $alt );
    $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, NULL );
    $attr = array_map( 'esc_attr', $attr );
    $html = sprintf( '<img src=".svg/2000px-WP_Suspension_logo.svg.png"', esc_url($url) );
    foreach ( $attr as $name => $value ) {
        $html .= " $name=" . '"' . $value . '"';
    }
    $html .= ' />';
    return $html;
}

This is what I'm doing now but it's throwing an error.

Filter, Hook:

post_thumbnail_html

Is there any hook to change the single product thumbnail? I did search a lot on SO as well as over the internet but no luck.

With 'thumbnail' I don't mean changing the size of the current image but I want to completely change/replace the product image (thumbnail) with a new image based on some scenario.

public function pn_change_product_image_link( $html, $post_id ){

    $url =  get_post_meta( $post_id );
    $alt = get_post_field( 'post_title', $post_id ) . ' ' .  __( 'thumbnail', 'txtdomain' );
    $attr = array( 'alt' => $alt );
    $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, NULL );
    $attr = array_map( 'esc_attr', $attr );
    $html = sprintf( '<img src="https://upload.wikimedia/wikipedia/commons/thumb/3/38/WP_Suspension_logo.svg/2000px-WP_Suspension_logo.svg.png"', esc_url($url) );
    foreach ( $attr as $name => $value ) {
        $html .= " $name=" . '"' . $value . '"';
    }
    $html .= ' />';
    return $html;
}

This is what I'm doing now but it's throwing an error.

Filter, Hook:

post_thumbnail_html
Share Improve this question asked Feb 15, 2018 at 9:12 Akash AgrawalAkash Agrawal 991 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Here's what I believe is an updated version of one of the other answers' comments. This function replaces the width and height attributes, but OP wanted to change the src. I imagine the solution would be similar, except using preg_replace to replace the src instead.

add_action('woocommerce_single_product_image_thumbnail_html','remove_single_product_image_attrs',10,2); 

function remove_single_product_image_attrs( $sprintf, $post_id ){ 
  return preg_replace( '/(width|height)="\d*"\s/', "", $sprintf ); 
} 

I hope this filter will help you.

apply_filters( 'woocommerce_single_product_image_thumbnail_html',  $sprintf,  $post_id ); 

Ref: http://hookr.io/filters/woocommerce_single_product_image_thumbnail_html/

发布评论

评论列表(0)

  1. 暂无评论