2016-11-29 46 views
-2

需要通過這有助於循環。我想爲循環中的第一個項目添加不同的樣式。獲得第一個項目在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(); ?>      
+0

omukiguy,看到我的答案。 – Ionut

+0

@ionut我想出來,但到目前爲止。它不顯示結果 – omukiguy

+0

好吧,那是另一個問題。你應該發表另外一個關於這個問題。我的代碼應該適用於這個問題。 – Ionut

回答

0

您可以添加計數器,並此基礎上,添加一個類的第一要素,你現在可以風格CSS該類:

<?php 
 
$prod_categories = get_terms('product_cat', array(
 
    'posts_per_page' => -1, 
 
    'orderby' => 'date', 
 
    'order' => 'ASC', 
 
    'hide_empty' => true 
 
)); 
 
$count = 0; 
 
$class = ''; 
 
foreach ($prod_categories as $prod_cat => $item) { 
 
    $count++; 
 
    if ($count == 1) { 
 
     $class = 'extra_class'; 
 
    }else{ 
 
     $class = ''; 
 
    } 
 
    $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'); 
 
?>    
 
      <div class="col-md-4 fig-hold <?php echo $class; ?>"> 
 
       <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(); 
 
?>

+0

謝謝,效果很好。我調整了一下以適應我的需要。 – omukiguy

+0

我很高興它的工作。 – Ionut

+0

我在循環後移動了變量並執行了類(Bootstrap) – omukiguy

0

這裏是我的最終代碼。謝謝@ionut

<?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) { 
     $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'); 

    $count++; 
    if ($count == 1) { $class = 'col-md-8'; } else{ $class = 'col-md-4'; } 
    ?> 
    <div class="<?php echo $class; ?> 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(); ?>