需要通过这有助于循环。我想为循环中的第一个项目添加不同的样式。获得第一个项目在PHP foreach循环
我曾尝试使用How to determine the first and last iteration in a foreach loop?和PHP - Grab the first element using a foreach不知它是不是wroking出来。上面的两个块也似乎分享应用代码的速度问题
查看代码中的注释以更好地理解我的问题。
<?php
$prod_categories = get_terms('product_cat', array('posts_per_page' => -1, 'orderby' => 'date', 'order' => 'ASC', 'hide_empty' => true));
foreach($prod_categories as $prod_cat => $item) {
$cat_thumb_id = get_woocommerce_term_meta($prod_cat->term_id, 'thumbnail_id', true);
$shop_catalog_img = wp_get_attachment_image_src($cat_thumb_id, 'shop_catalog');
$term_link = get_term_link($prod_cat, 'product_cat'); ?>
<!-- if first item echo
<div class="col-md-8 fig-hold">
<a href="<?php echo $term_link; ?>" style="text-decoration: none;">
<div class="figure">
<img src="<?php echo $shop_catalog_img[0]; ?>" class="img-responsive" alt="<?php echo $prod_cat->name; ?>" />
<div class="figureName"><?php echo $prod_cat->name; ?></div>
</div>
</a>
</div>
else -->
<div class="col-md-4 fig-hold">
<a href="<?php echo $term_link; ?>" style="text-decoration: none;">
<div class="figure">
<img src="<?php echo $shop_catalog_img[0]; ?>" class="img-responsive" alt="<?php echo $prod_cat->name; ?>" />
<div class="figureName"><?php echo $prod_cat->name; ?></div>
</div>
</a>
</div>
<?php } wp_reset_query(); ?>
omukiguy,看到我的答案。 – Ionut
@ionut我想出来,但到目前为止。它不显示结果 – omukiguy
好吧,那是另一个问题。你应该发表另外一个关于这个问题。我的代码应该适用于这个问题。 – Ionut