2013-10-02 75 views
0

即使有更多的缩略图,是否可以获取第一个缩略图?获取WooCommerce中的第一个缩略图

这是WooCommerce”代码来获取缩略图:

<?php 
/** 
* Single Product Thumbnails 
* 
* @author  WooThemes 
* @package  WooCommerce/Templates 
* @version  2.0.3 
*/ 

if (! defined('ABSPATH')) exit; // Exit if accessed directly 

global $post, $product, $woocommerce; 

$attachment_ids = $product->get_gallery_attachment_ids(); 

if ($attachment_ids) { 
    ?> 

    <div class="row"> 
     <div class="large-12 columns"> 

    <ul class="large-block-grid-4 thumbnails"> 

    <?php 

     $loop = 0; 
     $columns = apply_filters('woocommerce_product_thumbnails_columns', 3); 

     foreach ($attachment_ids as $attachment_id) { 

      $classes = array('zoom'); 

      if ($loop == 0 || $loop % $columns == 0) 
       $classes[] = 'first'; 

      if (($loop + 1) % $columns == 0) 
       $classes[] = 'last'; 

      $image_link = wp_get_attachment_url($attachment_id); 

      if (! $image_link) 
       continue; 

      $image  = wp_get_attachment_image($attachment_id, apply_filters('single_product_small_thumbnail_size', 'shop_thumbnail')); 
      $image_class = esc_attr(implode(' ', $classes)); 
      $image_title = esc_attr(get_the_title($attachment_id)); 

      echo apply_filters('woocommerce_single_product_image_thumbnail_html', sprintf('<li><a href="%s" class="%s" title="%s" rel="slb prettyPhoto[product-gallery]">%s</a></li>', $image_link, $image_class, $image_title, $image), $attachment_id, $post->ID, $image_class); 

      $loop++; 
     } 

    ?> 

    </ul> 

    </div> 
    </div> 

    <?php 
} ?> 

我已经找到了解决隐藏与CSS所有缩略图,但我想保持我的代码清洁。所以我更喜欢用WooCommerce来做到这一点。

回答

1

怎么样像;

<?php 
$attachment_id = 8; // attachment ID 

$image_attributes = wp_get_attachment_image_src($attachment_id); // returns an array 
if($image_attributes) { 
?> 
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"> 
<?php } ?>