Search code examples
phploopsforeachidentifier

Unique identifier in loop how to


I am trying to apply a unique identifier to each item in my loop. below is the sample from my loop and I am using it to show the fancybox lightbox.

<?php function my_feed() {

          $feed = fetch_feed( 'http://somewhere.com/feed' );
      if ( ! is_wp_error( $feed) ):

      // Get a maximum of 5 items

      $maxitems = $feed->get_item_quantity( 5 );

      $items = $feed->get_items( 0, $maxitems );

      foreach ( $items as $item ):

      ?>

      <div id="listing" class="twelve columns">


          <div class="four columns">

                <img src="#" height="200" width="200" /> <br/>

          </div>

          <div class="eight columns border">


              <!-- need this to be #more-info1. #more-info2, and so on -->
              <a href="#more-info" class="fancybox"><?php echo $item->get_title(); ?></a>


              <span class="rss-date"><?php echo $item->get_date( 'F j, Y' ); ?></span>

          </div>

      </div>

      <!-- this will coincide with the anchor so the id should be more-info1, more-info2, and so on -->
      <div id="more-info" style="display:none;width:auto;">
          <h3><?php echo $item->get_title(); ?></h3>
          <p>

              <?php echo $item->get_date( 'F j, Y' ); ?>

          </p>
          <p>
           <?php echo $item->get_description(); ?>
           <?php echo '<a href="'. $item->get_permalink().'>Link Here</a>'; ?>
           </p>
      </div>

      <?php

      endforeach;

      else: // Returned WP_Error, unable to fetch the feed. ?>

      <p>There was an error</p>

      <?php endif; ?>


      <?php
      }  ?>

So when I am looking to have a unique number after "#more-info" the href in the anchor tag like "#more-info1" and "#more-info2" and so on for the anchor tag. Then it will have a matching div with an id of "more-info1" and "more-info2" and so on. I know that it should run something like $myvariable++ but honestly I cant get it to work and have no idea on what to do.

Thanks for your help


Solution

  • Try this:

    $i = 1;
    foreach ( $items as $item ):
    ?>
    <a href="#more-info<?php echo $i++; ?>" class="fancybox">
    <?php
    endforeach;