Echo count out within foreach loop

How can I echo out the count of within a foreach loop? I want to change the class of the div below so it’s menu-button-1, menu-button-2 etc etc:

    <?php 
$pages = get_children(array('orderby' => 'menu_order', 'order' => 'asc'));
foreach($pages as $post) {
setup_postdata($post);
$fields = get_fields();
?>

    <div class="menu-button-(insert counter here)">
        <a href="<?php echo get_page_link( $post->ID ); ?>"><?php echo $post->post_title; ?></a>
    </div>
<?php
}
wp_reset_query();
?>

So I want it to output something like this – <div class="menu-button-1"> then <div class="menu-button-2"> and so on each time it goes through the loop.

Related posts

Leave a Reply

1 comment

  1.     <?php 
    $pages = get_children(array('orderby' => 'menu_order', 'order' => 'asc'));
    $i=1;
    foreach($pages as $post) {
    setup_postdata($post);
    $fields = get_fields();
    ?>
    
        <div class="menu-button-$i">
            <a href="<?php echo get_page_link( $post->ID ); ?>"><?php echo $post->post_title; ?></a>
        </div>
    <?php
    $i++;
    }
    wp_reset_query();
    ?>