【问题标题】:Change woocommerce images by image url from custom field通过自定义字段中的图像 url 更改 woocommerce 图像
【发布时间】:2018-05-01 18:57:22
【问题描述】:

我正在尝试建立一个会员网站,我需要在其中导入 10.000 多种产品。我使用 WP All Import,它让我可以选择将图片下载到我的服务器。但是你可以想象,所有的图片一起填满了我的空间,使我的网站和导入速度变慢。

我可以将图像 URL 保存为自定义字段,并且我想在我的 img 标记中使用此 URM,但我不知道如何完成此操作。我试过谷歌搜索,但我的问题太具体了。我没有得到构建模板的所有神秘代码。我认为这就像替换 img 标签的 src 一样简单,但我什至找不到 img 标签来做到这一点。我可以想象,如果很难找到 img 标签,那么用自定义字段替换 src 值会更加困难。

谁能告诉我如何实现我的目标?

【问题讨论】:

    标签: wordpress woocommerce wordpress-theming woocommerce-theming


    【解决方案1】:

    这没有很好的记录,但您可以使用woocommerce_placeholder_img 过滤器。当产品没有附加图像时,将调用此函数。

    add_filter( 'woocommerce_placeholder_img', 'replace_woocommerce_image' );
    
    function replace_woocommerce_image( $size ) {
    
        //we will need access to the global post object
        //this way we know which product to replace for
        global $post;
    
        //check for the custom image url
        $src = get_post_meta( $post->ID, '_image_url', true );
    
        //if not image url is found, use the default
        if( ! $src )
            $src = wc_placeholder_img_src();
    
        return '<img src="' . $src . '" />';
    }
    

    您可以在法典here 中看到这一点。看来您也可以使用woocommerce_placeholder_img_src 过滤器,它只会过滤源网址,而不是整个&lt;img&gt; 标记。

    祝你好运!

    【讨论】:

    • 非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2017-09-02
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多