代码之家  ›  专栏  ›  技术社区  ›  Zac

在PHP(Wordpress)循环中,如何生成唯一数字序列?

  •  1
  • Zac  · 技术社区  · 16 年前

    如果在循环中生成一个列表,例如:

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <div id="slide">
    <ul>
    <li class="slide-<?php //how can I generate an integer here ?>">
    <a href="#">stuff</a></li>
    </ul>
    </div>
    

    我如何在这些类和/或那些href中添加一个序列号?

    在这个例子中,幻灯片1,幻灯片2,等等。。

    2 回复  |  直到 16 年前
        1
  •  4
  •   realmfoo    16 年前
    <?php 
    if ( have_posts() ) :
        $slideNumber = 1;
        while ( have_posts() ) : the_post(); ?>
    <div id="slide">
    <ul>
    <li class="slide-<?php echo $slideNumber++; ?>">
    <a href="#">stuff</a></li>
    </ul>
    </div>
    <?php
        endwhile;
    endif; ?>
    
        2
  •  0
  •   Claudio    16 年前

    我在考虑你在同一页放几张幻灯片。所以我会把所有的HTML代码放在PHP代码中。类似于:

    <?php
        while ($count <= $total) {
            echo "<div id=\"slide\">";
            echo "<ul>";
            echo "<li class=\"slide-".$count."\">";
            echo "<a href=\"#\">stuff</a></li>";
            echo "</ul>";
            echo "</div>";
    
            count++;
        }
    ?>
    

    我没有检查代码,但这只是个主意。

    祝你好运。