将增量添加到CSS类

问题描述 投票:2回答:2

我需要能够为类名添加增量。我已经为此寻找解决方案,但它们并不适合我的例子。

我有一个foreach循环,我想在类'section_0'中添加一个序列号1,2,3,4等。我有什么想法我做错了吗?

<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 -->
php css increment
2个回答
3
投票

你可能想要进一步设置你的$counter。所以 -

$counter = 1;

在某些地方可能会更好

if( have_rows('section_container') ):

您可以在不同的地方尝试它,看看它如何最适合您的设置。

另外 - 在代码的这一部分,你的增量$counter两次(使用$counter++)。

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

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

你可以删除其中一个,你应该得到你的编号。


0
投票

$counter变量必须在没有++的情况下回显:

<div id="section_0<?php echo $counter; ?>" class="tabcontent">
© www.soinside.com 2019 - 2024. All rights reserved.