【问题标题】:Echo count out within foreach loop在 foreach 循环中回显计数
【发布时间】:2011-08-30 12:48:30
【问题描述】:

如何在 foreach 循环中回显计数?我想更改下面 div 的类,使其为 menu-button-1、menu-button-2 等:

    <?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();
?>

所以我希望它在每次循环时输出类似的内容 - &lt;div class="menu-button-1"&gt; 然后 &lt;div class="menu-button-2"&gt; 等等。

【问题讨论】:

    标签: php wordpress foreach


    【解决方案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();
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 2016-06-06
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多