【问题标题】:Retrieving product gallery image thumbnails in Woocommerce在 Woocommerce 中检索产品图库图像缩略图
【发布时间】:2019-04-18 16:29:46
【问题描述】:

我正在寻找一种功能,它可以让我获得画廊图像的缩略图,而不是完整的图像。我目前使用$product->get_gallery_attachment_ids(); 检索它们。

有问题的代码:

$attachment_ids = $product->get_gallery_attachment_ids();

$image_link = wp_get_attachment_url( $attachment_ids[0] );

echo "<img class='back-thumb' src='" . $image_link . "'></img>";

【问题讨论】:

    标签: php wordpress woocommerce product gallery


    【解决方案1】:

    您可以使用专用的 Woocommerce wc_get_gallery_image_html() 函数和 WC_Product 方法 get_gallery_image_ids() 喜欢:

    if ( $attachment_ids = $product->get_gallery_image_ids() ) {
        foreach ( $attachment_ids as $attachment_id ) {
            echo wc_get_gallery_image_html( $attachment_id );
        }
    }
    

    或喜欢你的代码:

    if ( $attachment_ids = $product->get_gallery_image_ids() )
        echo wc_get_gallery_image_html( $attachment_ids[0] );
    

    经过测试并且有效。


    更改缩略图尺寸和裁剪: See this documentation section

    【讨论】:

    • 这行得通,但顺便说一句,您知道如何使函数返回具有不同纵横比的图像吗? (以匹配商店页面上的主要产品图片)。现在,它显示的是 1:1 图片,尽管缩略图设置为 3:4 纵横比。
    • @opportunityr 我添加了a link to the related documentation,您可以在其中找到更改此行为的方法,使用Woocommerce 提供的可用设置或代码sn-p
    • @LoicTheAztec 谢谢。这对我帮助很大。我正在努力获取画廊图像。我对此很陌生,您介意分享一些特定于 Woocommerce 的资源吗?我相信它会帮助很多像我这样的新手。再次感谢!
    猜你喜欢
    • 2021-11-10
    • 2019-02-09
    • 2020-11-22
    • 1970-01-01
    • 2019-02-04
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多