2014-12-25 45 views
1

好的我有一個投資組合頁面。每個投資組合項目都附有縮略圖。我想要的是從第一篇文章中打印出第一個縮略圖兩次。一開始&其他在結尾處。如:image_1,image_2,image_3,image_4 &然後再image_1。如何在WordPress循環中輸出兩次縮略圖

我的代碼是:

<div class="main-interior portfolio" id="portfolio-big-pics" style="display: block;"> 
<?php $args = array('post_type' => 'portfolio', 'order' => 'dsc'); 
$loop = new WP_Query($args); 
while ($loop->have_posts()) : $loop->the_post(); ?> 

<?php $extraFirstClass = $loop->current_post == '0' ? ' main-image-porfolio-main' : ''; ?> 

<?php 
$attributes = array(
    "class" => "main-image portfolio " . $extraFirstClass, 
    "id" => "photo_{$post->ID}", 
); 
the_post_thumbnail("portfolio_thumb", $attributes); 
?> 

<?php endwhile; ?> 

<?php rewind_posts(); ?> 


    <div class="portfolio-box"> 
     <h5>Portfolio</h5> 
     <ul class="item-list" id="portfolio-list"> 
     <?php while ($loop->have_posts()) : $loop->the_post(); ?> 
     <li><a href="<?php the_permalink(); ?>" rel="<?php echo $post->ID; ?>"><?php the_title(); ?></a> 
     </li> 
     <?php endwhile; ?>    
     </ul> 
    </div> 

</div> 

我該怎麼辦呢?

回答

1

如之前我here回答,我會用PHP for。但是,如果由於某種原因你不想這樣做,我認爲你不需要爲此採用兩個循環。你可以做到以下幾點:

<?php 
$args = array('post_type' => 'portfolio', 'order' => 'dsc'); 
$loop = new WP_Query($args); 
$first = $loop->posts[0]; 
$attributes = array(
    "class" => "main-image portfolio", 
    "id" => "photo_{$loop->posts[$i]->ID}", 
); 

<div class="main-interior portfolio" id="portfolio-big-pics" style="display: block;"> 
    for($i = 0; $i < count($loop->posts); $i++) { 
     $attrs = $attributes; 
     $attrs["class"] .= $i === 0 ? ' main-image-portfolio-main' : ''; 
     echo get_the_post_thumbnail($loop->posts[$i]->ID, "portfolio_thumb", $attrs); 
    } 
    echo get_the_post_thumbnail($first->ID, "portfolio_thumb", $attrs); 
    ?> 

    <div class="portfolio-box"> 
     <h5>Portfolio</h5> 
     <ul class="item-list" id="portfolio-list"> 

      <?php while ($loop->have_posts()) : $loop->the_post(); ?> 
      <li> 
       <a href="<?php the_permalink(); ?>" rel="<?php echo $post->ID; ?>"><?php the_title(); ?></a>    
      </li> 
      <?php endwhile; ?> 

      <li> 
       <a href="<?php echo get_permalink($first->ID);?>" rel="<?php echo $first->ID; ?>"><?php echo $first->post_title;?></a> 
      </li> 
     </ul> 
    </div> 
</div> 
+0

不工作...我的佈局brokes如果我用你的代碼... –

+0

縮略圖應該是「組合框」分區之外! –

+0

,但我從你的代碼中得到了一些想法..我試試,如果我面臨任何問題,我會讓你知道!非常感謝你的幫助! –

-1

這裏是代碼,根據您的需要編輯:

$(document).ready(function() 
{ 
    var imagesrc = $(".example post's image class").attr("src"); 
    $(".the container or post after which you want to put your image").appendTo("<img src='"+imgsrc+"' alt='image' />"); 
});