【问题标题】:product gallery attachment url in woocommercewoocommerce中的产品库附件网址
【发布时间】:2015-11-27 13:43:31
【问题描述】:

我正在使用这个 sn-p 在 woocommerce 网站的自定义 wordpress 模板上输出产品库附件。我正在为弹出窗口使用灯箱。但我努力获取附件网址,而是继续使用弹出窗口的特色图片。

<?php
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();

$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail_size' );
$url = $thumb['0']; 


echo '<div>';
foreach( $attachment_ids as $attachment_id ) 
{
echo '<a href="' . $url . '" rel="shadowbox" >' ."<img src=".$image_link = wp_get_attachment_url( $attachment_id, 'large')." style='width:70px; height:70px;' >". '</a>';
 }
echo '</div>';
?>

关于如何为产品图库图片定位正确的 url 路径有什么想法吗? 非常感谢任何帮助!

【问题讨论】:

  • 你试过了吗:$image_link = wp_get_attachment_image_src($attachment_id, 'large'); ?
  • 嗨,我已经尝试过 'a href ' 部分,但它没有输出任何内容。很奇怪,因为它使用它作为图像 src 参考,它确实有效......
  • 你试过添加全局 $post;低于全球 $product; ?我认为是因为它在“循环”之外。
  • 这是有问题的页面。 airloom.co.za/product/kelim-bright右侧有一个缩略图库,当点击它时,它会弹出主要特色图像而不是图库图像..
  • 查看我的答案的更新编辑。

标签: php wordpress woocommerce gallery


【解决方案1】:

我已将'thumbnail_size' 更改为'large' 添加global $post; 并更改.wp_get_attachment_image_src( $attachment_id, 'large').

$post 需要在全局范围内进行 decaler 才能访问其内容,因为这在“循环”之外。

EDIT 2 我已经更新了下面的代码,所以它应该链接到点击的图像。删除了 $thumb$url,因为它没有被使用。

<?php
global $product;
global $post;

$attachment_ids = $product->get_gallery_attachment_ids();

echo '<div>';

foreach( $attachment_ids as $attachment_id ) 
{
    echo '<a href="' .wp_get_attachment_image_src( $attachment_id, 'large'). '" rel="shadowbox" >' ."<img src=".wp_get_attachment_image_src( $attachment_id, 'large')." style='width:70px; height:70px;' >". '</a>';
}

echo '</div>';
?>

【讨论】:

  • 感谢您的帮助,但这给了我一个断开的缩略图链接。
【解决方案2】:

希望对你有帮助

  global $product;
     $attachment_ids = $product->get_gallery_attachment_ids();

    foreach( $attachment_ids as $attachment_id ) 
    {
      //Get URL of Gallery Images - default wordpress image sizes
      echo $Original_image_url = wp_get_attachment_url( $attachment_id );
      echo $full_url = wp_get_attachment_image_src( $attachment_id, 'full' )[0];
      echo $medium_url = wp_get_attachment_image_src( $attachment_id, 'medium' )[0];
      echo $thumbnail_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' )[0];

      //Get URL of Gallery Images - WooCommerce specific image sizes
      echo $shop_thumbnail_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_thumbnail' )[0];
      echo $shop_catalog_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_catalog' )[0];
      echo $shop_single_image_url = wp_get_attachment_image_src( $attachment_id, 'shop_single' )[0];

      //echo Image instead of URL
      echo wp_get_attachment_image($attachment_id, 'full');
      echo wp_get_attachment_image($attachment_id, 'medium');
      echo wp_get_attachment_image($attachment_id, 'thumbnail');
      echo wp_get_attachment_image($attachment_id, 'shop_thumbnail');
      echo wp_get_attachment_image($attachment_id, 'shop_catalog');
      echo wp_get_attachment_image($attachment_id, 'shop_single');
    }

【讨论】:

    猜你喜欢
    • 2014-07-09
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2019-03-25
    • 2019-07-18
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多