2016-04-27 6 views
0

現在、製品で構成されているスライダを作成しています。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> 

このコードは次のように2つのずつの製品をラップ: 現在、私はこのコードを使用してdiv要素内のすべての2つの項目をラップするために管理している

<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

はい私は値を変更しましたが、うまくいきません。 –

関連する問題