Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 哪个钩子用于Woocommerce单产品缩略图src链接替换_Php_Wordpress_Woocommerce_Product_Hook Woocommerce - Fatal编程技术网

Php 哪个钩子用于Woocommerce单产品缩略图src链接替换

Php 哪个钩子用于Woocommerce单产品缩略图src链接替换,php,wordpress,woocommerce,product,hook-woocommerce,Php,Wordpress,Woocommerce,Product,Hook Woocommerce,是否有任何钩子来更改单个产品缩略图?我确实在SO上搜索了很多,也在互联网上搜索了很多,但是运气不好 对于“缩略图”,我不是指更改当前图像的大小,而是希望根据某些场景使用新图像完全更改/替换产品图像(缩略图) public function pn_change_product_image_link( $html, $post_id ){ $url = get_post_meta( $post_id ); $alt = get_post_field( 'post_title',

是否有任何钩子来更改单个产品缩略图?我确实在SO上搜索了很多,也在互联网上搜索了很多,但是运气不好

对于“缩略图”,我不是指更改当前图像的大小,而是希望根据某些场景使用新图像完全更改/替换产品图像(缩略图)

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.org/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;
}
你可以试试这个-

// define the woocommerce_single_product_image_thumbnail_html callback 
function filter_woocommerce_single_product_image_thumbnail_html( $sprintf, $post_id ) { 
    // make filter magic happen here... 
    // here you have product id too, fetch your data or re evalute things and return final image HTML
    return $sprintf; 
}; 

// add the filter 
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'filter_woocommerce_single_product_image_thumbnail_html', 10, 2 ); 

在模板
single product/product image.php
上,您可以看到在single product页面上显示图像的所有源代码

正如我在评论中所建议的,您可以使用
woocommerce\u single\u product\u image\u thumbnail\u html
,但这并不是真正有用的,因为您希望为主图像设置自定义源链接

在模板源代码中,您将看到它使用WordPress函数
wp\u get\u attachment\u image\u src()
,在它的源代码中,您将看到可以使用过滤器挂钩
“wp\u get\u attachment\u image\u src”
,这对于您想要做的事情来说非常方便

现在,您可以在此基础上构建代码:

add_filter( 'wp_get_attachment_image_src', array( $this, 'pn_change_product_image_link'), 50, 4 );
public function pn_change_product_image_link( $image, $attachment_id, $size, $icon ){
    if( ! is_product() ) return $image; // Only on single product pages
    if( $size == 'shop_thumbnail' ) return $image; // Not for gallery thumbnails (optional)

    // Your source image link
    $src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/WP_Suspension_logo.svg/2000px-WP_Suspension_logo.svg.png';
    $width  = ''; // <== (optional) define the width
    $height = ''; // <== (optional) define the height
    $image  = array( $src, $width, $height );

    return $image;
}
由此

add_filter( 'wp_get_attachment_image_src', 'pn_change_product_image_link', 50, 4 );

此代码位于活动子主题(或主题)的function.php文件中。

应用过滤器('woocommerce\u single\u product\u image\u thumbnail\u html',$html,get\u post\u thumbnail\u id($post->id))
在模板上
单个产品/产品图像.php
…您可以使用
全局$post,$product在钩住的函数中additionally@Akash,请参阅下面的链接,这将很有帮助。缩略图是一组图像,它们不同于主产品图像,检查woocommerce模板
product thumbnails.php
product image.php
,查看可以在何处修改和添加filter@LoicTheAztec我正在开发一个插件,产品的图像应该由我的插件更新,这样我就不能编辑任何模板文件。@silver,我正在开发一个插件,该产品的图像应该由我的插件更新,所以我不能编辑任何模板文件。惊人的解释!!这很有魅力。非常感谢你的帮助。我真的很感激。
add_filter( 'wp_get_attachment_image_src', array( $this, 'pn_change_product_image_link'), 50, 4 );
add_filter( 'wp_get_attachment_image_src', 'pn_change_product_image_link', 50, 4 );