2016-04-27 68 views
0

我目前正在創建一個由產品組成的滑塊。 目前,我已成功使用此代碼每2項包裝在格:PHP在循環Wordpress中包裝自定義項目

<div class="frame crazy" id="crazy"> 
    <div class="slidee"> 
    <?php if ($myposts->have_posts()) : 
    $i = 0; 
    while ($myposts->have_posts()) : $myposts->the_post();  
    $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_id()), 'single-post-thumbnail'); 
    $value = get_field("alternate_image", get_the_id()); 
    $titl = get_field("title", get_the_id()); 
    $big = get_field("big_section", get_the_id()); 
    if ($i % 2 == 0) : ?> 
     <div class="op" <?if ($big==1){?>style="width:850px !important;"<?}?>> 
     <?php endif; 
     $_pf = new WC_Product_Variable(get_the_id()); 
     $variations =  $_pf->get_available_variations(); 
     $vrt = count($variations); 
     ?> 
     <div data-hv="<?php echo $value; ?>" data-titleContent="<a href='<?php echo get_the_permalink();?>'><?php echo get_the_title(); ?></a>" data-tipso-content="<span class='varaition'>this item has <?php echo $vrt; ?> variation(s)</span><a class='bty' href='<?php echo get_the_permalink(); ?>'>details</a>" data-url="<? echo the_permalink(); ?>" class="cola <?php if($big==1){?>big<?}?>" style="background-image: url('<?php echo $image[0]; ?>')" data-mg="<?php echo $image[0];?>"> 
     <?php if($titl==1) { ?> 
     <h2><a href='<?php echo get_the_permalink();?>'><?php echo get_the_title(); ?></a></h2> 
     <p class="slu"><a href='<?php echo get_the_permalink();?>'>shop now ></a> </p> 
     <?php } ?> 
      </div> 
     <?php if ($i % 2 != 0) : ?> 
     </div> 
     <?php endif; ?> 

     <?php $i++; endwhile; ?> 

      <?php if ($i % 2 != 0) : ?> 
       </div> 
      <?php endif; ?> 

    <?php endif;?> 
    </div> 
    </div> 

此代碼每兩個產品包裝是這樣的:

<div class="op"> 
<div class="product1"> 
//content 
</div> 
<div class="product2"> 
//content 
</div> 
</div> 
<div class="op"> 
<div class="product3"> 
//content 
</div> 
<div class="product4"> 
//content 
</div> 
</div> 

但我需要獲取根據崗位的自定義數字產品元。所以產品的數量可以是這樣變化:

<div class="op"> 
<div class="product1"> 
//content 
</div> 
<div class="product1"> 
//content 
</div> 
<div class="product1"> 
//content 
</div> 
<div class="product1"> 
//content 
</div> 
<div class="op"> 
<div class="product1"> 
//content 
</div> 
<div class="product1"> 
//content 
</div> 
</div> 

是否有可能使用產品元或任何更好的想法?

回答

0

這一行代碼:

if ($i % 2 == 0) : ?> 

包含循環次數,改變是一個變量,然後從元數據設置變量,所以:

$loopmeta=metadata_retriever(); 
if ($i % $loopmeta == 0) : ?> 

你將不得不編寫metadata_retriever()函數,並在檢索$loopmeta變量時進行一些錯誤檢查,以確保它作爲有效整數返回(不是0,也不是12.735,例如:-))

+0

我試過使用這個,但如果我改變2的值,它不工作。它只適用於2種產品。我正在創建一個像[this]一樣的滑塊(http://www.gucci.com/int/category/m/travel_bags___luggage#look86083lookA113),它由動態數量的項目組成! –

+0

所以,你是否在你的代碼中的任何地方改變了'2'的值,你使用它來變量數量的值?因爲您在發佈的代碼中檢查了3次。我會建議重構這個,所以你不要混合使用PHP和HTML。使用函數來創建輸出字符串並將它們連接在一起,然後在最後輸入'print $ str'。那麼你會看到錯誤和優化的可能性。 – Cwissy

+0

是的,我改變了值,但它沒有工作。 –