2013-10-13 47 views
0

我有我的滑塊,工作正常。我的主要問題是我正在使用的循環,可以更簡化嗎?填充滑塊與PHP循環的類別精選圖像

我正在填充滑塊的類別,其中帖子附加到與精選圖像。它提取了精選圖片以及帖子的標題,作者和簡單的閱讀更多按鈕。

我與滑塊

<div class="slider"> 
    <ul class="slide"> 
     <li> 
      <?php query_posts('showposts=1&cat=48'); ?> 
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
        <?php echo get_the_title(); ?> 
         <div class="latest-post"> 
          <a href="<?php the_permalink() ?>" rel="bookmark"> 
           <?php the_post_thumbnail(); echo '<p>read more</p>'; ?> 
          </a> 
         </div> 
        <p>Other posts by <?php the_author_posts_link(); ?></p>    
       <?php endwhile; endif; ?> 
      <?php wp_reset_query(); ?> 
     </li> 
     <li> 
      <?php query_posts('showposts=1&cat=49'); ?> 
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
        <?php echo get_the_title(); ?> 
         <div class="latest-post"> 
          <a href="<?php the_permalink() ?>" rel="bookmark"> 
           <?php the_post_thumbnail(); echo '<p>read more</p>'; ?> 
          </a> 
         </div> 
        <p>Other posts by <?php the_author_posts_link(); ?></p>    
       <?php endwhile; endif; ?> 
      <?php wp_reset_query(); ?> 
     </li> 
     <li> 
      <?php query_posts('showposts=1&cat=50'); ?> 
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
        <?php echo get_the_title(); ?> 
         <div class="latest-post"> 
          <a href="<?php the_permalink() ?>" rel="bookmark"> 
           <?php the_post_thumbnail(); echo '<p>read more</p>'; ?> 
          </a> 
         </div> 
        <p>Other posts by <?php the_author_posts_link(); ?></p>    
       <?php endwhile; endif; ?> 
      <?php wp_reset_query(); ?> 
     </li> 
     <li> 
      <?php query_posts('showposts=1&cat=51'); ?> 
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
        <?php echo get_the_title(); ?> 
         <div class="latest-post"> 
          <a href="<?php the_permalink() ?>" rel="bookmark"> 
           <?php the_post_thumbnail(); echo '<p>read more</p>'; ?> 
          </a> 
         </div> 
        <p>Other posts by <?php the_author_posts_link(); ?></p>    
       <?php endwhile; endif; ?> 
      <?php wp_reset_query(); ?> 
     </li> 
    </ul> 
</div> 

回答

1

環您可以通過使用一個for循環壓縮。

<div class="slider"> 
<ul class="slide"> 
    <?php for ($i=48;$i<52;$i++) { ?> 
     <li> 
      <?php query_posts('showposts=1&cat=' . $i . ''); ?> 
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
        <?php echo get_the_title(); ?> 
         <div class="latest-post"> 
          <a href="<?php the_permalink() ?>" rel="bookmark"> 
           <?php the_post_thumbnail(); echo '<p>read more</p>'; ?> 
          </a> 
         </div> 
        <p>Other posts by <?php the_author_posts_link(); ?></p>    
       <?php endwhile; endif; ?> 
      <?php wp_reset_query(); ?> 
     </li> 
    <?php } ?> 
</ul> 

+0

確定生病有一個補鍋匠。通過類別48-52是否正確?這很好,謝謝隊友 – hputtick

+0

是的。我相信有一個更好的方法來做到這一點。 –

+0

我會暫時使用你的答案,除非有人提出另一個想法。再次感謝 – hputtick