【问题标题】:Show first image gallery product woocommerce显示第一个图片库产品 woocommerce
【发布时间】:2018-08-06 07:35:48
【问题描述】:

我在尝试在 woocommerce 上显示图库产品的第一张图片时遇到了一些问题,

我正在开发一个带有个人模板的个人 wordpress 主题,所有这些工作都在猫头鹰轮播中,以在不同的幻灯片上展示每个产品,

我是 Woocommerce 的新手,我正在使用 WP_Query 显示某个类别的产品,在里面我会暂时显示每个产品的 the_title、the_content、the_permalink,我想显示第一张也是唯一一张图片在每个产品的画廊内,但我不知道如何找到它:

<div class="owl-carousel owl-theme" id="carousel">
        <?php 
          $params = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'posts_per_page'        => '-1',
            'product_cat'           => 'barras'
          );

          $wc_query = new WP_Query($params);
          if ($wc_query->have_posts()) :
            while ($wc_query->have_posts()) :
              $wc_query->the_post();
              ?>
              <div class="item">
                <a href="<?php the_permalink();?>">
                  <a href="javascript:void(0)" class="prev"><img src="<?php echo get_template_directory_uri(); ?>/assets/img/prev.png" alt=""></a>
                  <a href="javascript:void(0)" class="next"><img src="<?php echo get_template_directory_uri(); ?>/assets/img/next.png" alt=""></a>
                  <p class="titulo-producto-slider"><?php the_title(); ?></p>
                </a>
                <div class="espacio-10"></div>
                <div class="descripcion-producto-slider">
                  <?php the_content(); ?>
                </div>
                <div class="ver-detalle">
                  <ul>
                    <li>
                      <a href="<?php the_permalink();?>">Ver detalles</a>
                    </li>
                  </ul>
                </div>
              </div>
          <?php
            endwhile;
              wp_reset_postdata();
          else:  ?>
            <p><?php _e( 'No Products' );?></p> 
          <?php endif;
        ?>
      </div>

对不起,如果你不明白,这是我在堆栈上的第一个问题,我不会说英语,

非常感谢,

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    试试这个:

    $product = new WC_Product( get_the_ID() );
    $attachment_ids = $product->get_gallery_image_ids();
    
    if ( is_array( $attachment_ids ) && !empty($attachment_ids) ) {
        $first_image_url = wp_get_attachment_url( $attachment_ids[0] );
        // ... then do whatever you need to do
    } // No images found
    else {
        // @TODO
    }
    

    更多详情:WP_Product class reference

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多