簡體   English   中英

循環中的唯一標識符如何

[英]Unique identifier in loop how to

我正在嘗試為我的循環中的每個項目應用唯一標識符。 下面是我的循環中的示例,我用它來顯示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 ++的東西,但老實說,我不能讓它工作,不知道該怎么做。

謝謝你的幫助

嘗試這個:

$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