2016-04-27 46 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

是的,我改变了值,但它没有工作。 –