简体   繁体   English

循环中的唯一标识符如何

[英]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. 下面是我的循环中的示例,我用它来显示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
      }  ?>

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. 因此,当我希望在“#more-info”之后有一个唯一的数字时,锚标记中的href如“#more-info1”和“#more-info2”等等,对于锚标记。 Then it will have a matching div with an id of "more-info1" and "more-info2" and so on. 然后它将有一个匹配的div,其id为“more-info1”和“more-info2”,依此类推。 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. 我知道它应该运行类似$ myvariable ++的东西,但老实说,我不能让它工作,不知道该怎么做。

Thanks for your help 谢谢你的帮助

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM