【问题标题】:Get the first thumbnail in WooCommerce获取 WooCommerce 中的第一个缩略图
【发布时间】:2013-10-08 22:21:51
【问题描述】:

是否有可能只获得第一个缩略图,即使附加了更多?

这是获取缩略图的 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 来做这件事。

【问题讨论】:

    标签: image wordpress thumbnails 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 } ?>
    

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2015-12-03
      相关资源
      最近更新 更多