2010-05-11 39 views
1

不同輸出的東西我運行一個插件叫做類別帖子的Widget爲WordPress:http://wordpress.org/extend/plugins/category-posts/While循環:每月的第二個結果

它使用while循環在某個類別,顯示所有職位的名稱。我想要得到它,以便在每個第二個輸出中都有一個不同的類附加到li標記。

這裏是代碼插件塊:

// Post list 
    echo "<ul>\n"; 

    while ($cat_posts->have_posts()) 
    { 
     $cat_posts->the_post(); 
    ?> 
     <li class="cat-post-item"> 
      <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> 

      <?php 
       if (
        function_exists('the_post_thumbnail') && 
        current_theme_supports("post-thumbnails") && 
        $instance["thumb"] && 
        has_post_thumbnail() 
       ) : 
      ?> 
       <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
       <?php the_post_thumbnail('cat_post_thumb_size'.$this->id); ?> 
       </a> 
      <?php endif; ?> 

      <?php if ($instance['date']) : ?> 
      <p class="post-date"><?php the_time("j M Y"); ?></p> 
      <?php endif; ?> 

      <?php if ($instance['excerpt']) : ?> 
      <?php the_excerpt(); ?> 
      <?php endif; ?> 

      <?php if ($instance['comment_num']) : ?> 
      <p class="comment-num">(<?php comments_number(); ?>)</p> 
      <?php endif; ?> 
     </li> 
    <?php 
    } 

    echo "</ul>\n"; 

我只是想獲得它等各個第二個在輸出列表中,李有不同的類,所以貓後item-alt例如。

感謝,

韋德

回答

0

以下是未經測試,但它說明的基本原則。只需在循環的每個實例上切換一個布爾值。即使帖子也會有類cat-post-item-even,所以你不必修改你的樣式表。

發現for循環可以用於除簡單增量之外的其他事情是很好用的。

這兩條編輯過的行被標記爲這樣。

// Post list 
echo "<ul>\n"; 

/* edited */ for($even=false;$cat_posts->have_posts();$even=!$even) 
{ 
    $cat_posts->the_post(); 
?> 
<!-- edited -->  <li class="cat-post-item<?php if($even) echo " cat-post-item-even"; ?>"> 
     <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> 

     <?php 
      if (
       function_exists('the_post_thumbnail') && 
       current_theme_supports("post-thumbnails") && 
       $instance["thumb"] && 
       has_post_thumbnail() 
      ) : 
     ?> 
      <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
      <?php the_post_thumbnail('cat_post_thumb_size'.$this->id); ?> 
      </a> 
     <?php endif; ?> 

     <?php if ($instance['date']) : ?> 
     <p class="post-date"><?php the_time("j M Y"); ?></p> 
     <?php endif; ?> 

     <?php if ($instance['excerpt']) : ?> 
     <?php the_excerpt(); ?> 
     <?php endif; ?> 

     <?php if ($instance['comment_num']) : ?> 
     <p class="comment-num">(<?php comments_number(); ?>)</p> 
     <?php endif; ?> 
    </li> 
<?php 
} 

echo "</ul>\n"; 
+0

非常感謝。幫助噸! – 2010-05-11 02:23:49

0
// .... 
<? $type = ($type + 1) % 2; ?> 
<li class="cat-post-item<?=$type ?>"> 
0
$counter=1; 

for ($i=0; $i<=10; $i++) { 

    if ($counter%3===0) { 
     echo 'something else'; 
    } else { 
     echo 'normal'; 
    } 

    echo '<br />'; 

    $counter++; 
} 
+0

難道你不是指$ counter%2嗎? – Simon 2010-05-11 02:23:49

+0

你說得對,我誤解了這個問題;他認爲他會修改001001 ..反對010101 ..其他方法:'if($ counter&1){}' – Alec 2010-05-11 02:39:06

相關問題