In a PHP (WordPress) loop how do you generate a sequence of unique numbers?

If I have a list being generated in a loop, something like:

<?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>

How could I append a sequential number to each of those classes, and/or those hrefs?

Read More

so in that example slide-1,slide-2, etc..

Related posts

Leave a Reply

2 comments

  1. <?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. I’m considering that you will put some slides in the same page. So I would put all HTML code in the PHP code. Something like that:

    <?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++;
        }
    ?>
    

    I didn’t check the code, but it’s just an idea.

    Good luck.