插入新的$counter
在foreach循环开始之前,以及在循环的最末端,调用$counter++
以增加数字。在中间,只需回显$counter
变量
<?php
$pages = get_children(array(\'orderby\' => \'menu_order\', \'order\' => \'asc\'));
// Add a counter before foreach starts
$counter = 1;
foreach($pages as $post) {
setup_postdata($post);
$fields = get_fields();
?>
<div class="menu-button-<?php echo $counter; /* echo out counter */ ?>">
<a href="<?php echo get_page_link( $post->ID ); ?>"><?php echo $post->post_title; ?></a>
</div>
<?php
$counter++; // increment before foreach ends
}
wp_reset_query();
?>