【问题标题】:woocommerce get image sizewoocommerce 获取图像大小
【发布时间】:2016-11-15 11:48:40
【问题描述】:

我正在尝试在 woocommerce 的单个产品页面上获取与产品相关的所有图像的尺寸(尺寸)。 我需要照片滑动“数据大小”值的值宽度和高度。

这就是我现在拥有的

<ul class="slides">
<?php
    $attachment_ids = $product->get_gallery_attachment_ids();
    $attachment_first[0] = get_post_thumbnail_id( $product->id );
    $attachment = wp_get_attachment_image_src( $attachment_first[0], 'full' );
    $img_size = wc_get_image_size( $attachment_ids, 'full' );
    $w = $img_size['width'];
    $h = $img_size['height'];
    $size = $w .'x'. $h;
?>
    <li class="picture">
        <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
            <a href="<?php echo $attachment[0]; ?>" itemprop="contentUrl" data-size="<?php echo $size; ?>">
                <img src="<?php echo $attachment[0]; ?>" itemprop="image" />
                <?php echo $size; ?>
            </a>
        </figure>
    </li>
<?php
    foreach( $attachment_ids as $attachment_id ){
        $src_url = wp_get_attachment_url( $attachment_id );
        echo '<li class="picture"><figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"><a href="' . $src_url . '" itemprop="contentUrl" data-size="' . $size . '"><img src="' . $src_url . '" itemprop="image" />'.$size .'</a></figure></li>';
    }
?>
</ul>

它正确地获取了所有图像,但是宽度和高度值是关闭的。我知道我正在测试的产品的完整图像是 800x800。如果我在

中删除“$attachment_ids”

wc_get_image_size( $attachment_ids, 'full' );

那么这个值就变成了300x300。

我真的需要找到一种简单的方法来从 woocommerce 获取全尺寸图像的实际尺寸。 我原以为这项任务会更简单:P

【问题讨论】:

    标签: php woocommerce dimensions getimagesize


    【解决方案1】:

    Nwm,用这个解决了(对于其他搜索这个的人)

    <ul class="slides">
    <?php
        $attachment_ids = $product->get_gallery_attachment_ids();
        $attachment_first[0] = get_post_thumbnail_id( $product->id );
        $attachment = wp_get_attachment_image_src( $attachment_first[0], 'full' );
        $w = $attachment[1];
        $h = $attachment[2];
        $size = $w .'x'. $h;
    ?>
        <li class="picture">
            <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
                <a href="<?php echo $attachment[0]; ?>" itemprop="contentUrl" data-size="<?php echo $size; ?>">
                    <img src="<?php echo $attachment[0]; ?>" itemprop="image" />
                </a>
            </figure>
        </li>
    <?php
        foreach( $attachment_ids as $attachment_id ){
            $src_url = wp_get_attachment_url( $attachment_id );
            $attachments = wp_get_attachment_image_src( $attachment_id, 'full' );
            $wp = $attachments[1];
            $hp = $attachments[2];
            $sizes = $wp .'x'. $hp;
            echo '<li class="picture"><figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"><a href="' . $src_url . '" itemprop="contentUrl" data-size="' . $sizes . '"><img src="' . $src_url . '" itemprop="image" /></a></figure></li>';
        }
    ?>
    </ul>
    

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 2012-07-20
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2011-12-15
      • 2018-07-11
      相关资源
      最近更新 更多