2013-10-20 52 views
0

我有一个显示HTML的while循环。更改while循环中的第一个元素?

但是,我只需要第一次迭代的不同元素。

我该如何管理?这是我的代码:

<?php 
if (have_posts()) : 
    while (have_posts()) : 
     the_post(); 
     $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); 
     ?> 

     <div class="item" style="background-size:cover; background-image: url(<?php echo $image[0]; ?>); width: 100%; min-height: 300px;"> 
      <div class="carousel-caption"> 
       <h3><?php the_title(); ?></h3> 
       <p><?php comments_number('Pas de commentaires', '1 commentaire', '% commentaires'); ?></p> 
      </div> 
     </div> 

     <?php 
    endwhile; 
endif; 
?> 

感谢您的帮助!

回答

3

试试这个代码

<?phph $flag = 1; ?> 

<?php if (have_posts()) : ?> 

    <?php while (have_posts()) : the_post(); ?> 

    <?php if($flag) { 

        DO YOUR WORK; 


        $flag = 0; 
        } ?> 

    Rest of your code. 

当该脚本将首次将发现,该标志为真,所以你可以做一些与第一循环不同运行。那么FLAG就会变成错误的。等下一个循环,它不会得到被用于第一仅环

1

你的完整代码这个“如果”里面:

<?php 
if (have_posts()) : 
    $flag = 1; 
    while (have_posts()) : the_post(); 
     if($flag == 1) { 

        PUT YOUR CODE HERE;  

        $flag = 0; 
        } 

     $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); 
     ?> 

     <div class="item" style="background-size:cover; background-image: url(<?php echo $image[0]; ?>); width: 100%; min-height: 300px;"> 
      <div class="carousel-caption"> 
       <h3><?php the_title(); ?></h3> 
       <p><?php comments_number('Pas de commentaires', '1 commentaire', '% commentaires'); ?></p> 
      </div> 
     </div> 

     <?php 
    endwhile; 
endif; 
?> 

感谢。

相关问题