循环中的唯一标识符如何

时间:2013-02-13 23:40:05

标签: php loops foreach identifier

我正在尝试为循环中的每个项目应用唯一标识符。下面是我的循环中的示例,我用它来显示fancybox灯箱。

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

因此,当我希望在“#more-info”之后有一个唯一的数字时,锚标记中的href如“#more-info1”和“#more-info2”等等,以便锚标记。然后它将有一个匹配的div,其id为“more-info1”和“more-info2”,依此类推。我知道它应该运行类似$ myvariable ++的东西,但老实说,我不能让它工作,不知道该怎么做。

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

试试这个:

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