【问题标题】:How to get Wordpress image_size url如何获取 Wordpress image_size url
【发布时间】:2013-08-15 01:06:33
【问题描述】:

我正在将 Woocommerce 用于 1800 多种产品的电子商店。 Woocommerce 带有一个内置的 PrettyPhoto 灯箱插件。默认情况下,当您单击放大灯箱中的图像时,它会加载原始 Wordpress 图像大小。

Woocommerce 在模板“product-image.php”中调用此原始图像。我可以修改那个。变量 $image_link 调用原始大小。我想调用我已经制作的自定义尺寸。我只是不知道怎么称呼它,我在文档、论坛和网站上找了一天多,但找不到。

我需要修改 $image_link 以便它调用我的自定义图像大小(我通过 add_image_size 添加的)。但是怎么做?我知道这是 Wordpress 基础知识,但我无法完成...

我的网站示例(点击放大,它显示了 1600 像素宽的缩小图像,但我想要一个更小的图像):http://www.affichesmarci.com/shop/the-railys-cycling-act/

<?php
    if ( has_post_thumbnail() ) {

        $image              = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );

        $image_title        = esc_attr( get_the_title( get_post_thumbnail_id() ) );
        $image_link         = wp_get_attachment_url( get_post_thumbnail_id() );

        $attachment_count   = count( $product->get_gallery_attachment_ids() );

        if ( $attachment_count > 0 ) {
            $gallery = '[product-gallery]';
        } else {
            $gallery = '';
        }

        echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="bigbox woocommerce-main-image zoom" title="%s" rel="prettyPhoto' . $gallery . '">%s</a>', $image_link, $image_title, $image ), $post->ID );

    } else {

        echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" class="bigbox" />', woocommerce_placeholder_img_src() ), $post->ID );

    }
?>

<?php do_action( 'woocommerce_product_thumbnails' ); ?>

【问题讨论】:

    标签: wordpress thumbnails woocommerce prettyphoto


    【解决方案1】:

    如果你已经在函数中设置了“add_image_size”,我想你可以很容易地编辑

    $image              = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
    

    $image              = get_the_post_thumbnail( $post->ID, apply_filters( 'your_thumbnail_name', 'shop_single' ) );
    

    您也可以使用 WP 的默认尺寸,即“缩略图”、“中”、“大”和“全”

    【讨论】:

    • 感谢您的建议,但没有成功。显然 'single_product_large_thumbnail_size' 不是图像大小。例如,将其更改为“中等”不会做任何事情。
    • 只是为了清楚。 $image 很好。它给了我正确的 image_size。我需要更改调用原始大小的 $image_link,我希望它是我创建的自定义大小。
    【解决方案2】:

    我自己找到了解决方案。

    调用全尺寸图像的原始变量。

    $image_link = wp_get_attachment_url( get_post_thumbnail_id() );
    

    调用自定义大小“缩放”的新变量。

    $image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'zoom' );
    

    【讨论】:

      【解决方案3】:

      好的,添加大尺寸而不是完整尺寸

      $image_link = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
      

      现在将$image_link 更改为$image_link[0]

      echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s"  rel="prettyPhoto' . $gallery . '">%s</a>', $image_link[0], $image_title, $image ), $post->ID );
      

      感谢您的想法:)

      【讨论】:

        猜你喜欢
        • 2014-07-11
        • 1970-01-01
        • 2021-04-13
        • 1970-01-01
        • 1970-01-01
        • 2021-08-01
        • 2012-06-30
        • 2018-06-28
        • 1970-01-01
        相关资源
        最近更新 更多