2017-12-20 19 views
1

クラス名にインクリメントを追加できる必要があります。私はこれのための解決策を探しましたが、私の例ではうまくいきません。CSSクラスにインクリメントを追加する

私は、クラス 'section_0'に連続番号1,2,3,4などを追加したいforeachループを持っています。私が間違っていることは何ですか?

<div class="menu-section-container"> 


<?php 

// check if the repeater field has rows of data 
if(have_rows('section_container')): 

// loop through the rows of data 
while (have_rows('section_container')) : the_row(); 

    if(have_rows('sub_section_container')): 

    // loop through the rows of data 
    while (have_rows('sub_section_container')) : the_row(); 

     if(have_rows('food_item')): 

     // loop through the rows of data 
     while (have_rows('food_item')) : the_row(); 

$menu_contents = array(the_sub_field('section_heading'), the_sub_field('item_name'), the_sub_field('price'), the_sub_field('item_description')); 

$counter = 1; 

foreach ($menu_contents as $menu_content); { ?> 

<div id="section_0<?php echo $counter++; ?>" class="tabcontent"> 
<?php echo $menu_content; ?> 
</div> 

<?php $counter++; // increment before foreach ends 
} 

     endwhile; 
     else : 
     endif; 

    endwhile; 
    else : 
    endif; 

endwhile; 
else : 
endif; 

wp_reset_postdata(); 

?> 

</div> <!-- section --> 
+0

出力はあなたの何を得ていますか? –

+0

@NigelRen Redクラス名と各グループの番号1を返します。したがって、基本的に "section_01"、 "section_01"、 "section_02"、 "section_03"などではありません。 – Paul

+0

インデントにはコード構造が反映されていることを確認してください。それはあなたにかなりの頭痛を救うでしょう。 – jcaron

答えて

3

おそらく$counterをさらに設定する必要があります。だから、 -

$counter = 1; 

は、おそらくあなたはそれがあなたのセットアップに最適な方法を確認するために別の場所でそれを試すことができます

if(have_rows('section_container')): 

後にどこかのような方が良いだろう。

また、コードのこのセクションでは、$counterを2回インクリメントしています($counter++を使用)。

<div id="section_0<?php echo $counter++; ?>" class="tabcontent"> 
<?php echo $menu_content; ?> 
</div> 

<?php $counter++; // increment before foreach ends 

これらのうちの1つを削除することができます。後で番号を付ける必要があります。

+0

最初の++は私が何かを試していたのですが、申し訳ありませんがポストの前にそれを削除する必要がありました。 $カウンターを動かすとうまくいった!ありがとう – Paul

0

$counter変数は++ずにエコーする必要があります。

<div id="section_0<?php echo $counter; ?>" class="tabcontent"> 
関連する問題