dongwo1914 2013-02-13 23:40
浏览 46
已采纳

循环中的唯一标识符如何

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

  • 写回答

1条回答 默认 最新

  • duandao8607 2013-02-13 23:44
    关注

    Try this:

    $i = 1;
    foreach ( $items as $item ):
    ?>
    <a href="#more-info<?php echo $i++; ?>" class="fancybox">
    <?php
    endforeach;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动