2012-03-23 73 views
0

嗨即時嘗試從類別5獲得第一篇文章,並作出兩個不同類。但是我所有的帖子都是「正規」的課程,我想要第一篇文章(只有第一篇文章)讓課程「有特色」,而其餘的課程「常規」不能弄清楚什麼是錯的。 HALP! :) 哦,順便說一句代碼工作,如果我刪除代碼的第一線,但隨後得到的所有帖子所有類別:/wordpress firstpost類別

<?php query_posts('cat=5'); 
    while (have_posts()) : the_post(); ?> 

    <?php if (is_paged()) : $postclass = ('regular'); ?> 
    <?php else : ?> 
    <?php $postclass = ($post == $posts[0]) ? 'featured' : 'regular'; ?> 
    <?php echo $postclass; ?> 
    <?php endif; ?> 
    <?php endwhile ?> 

回答

0

爲什麼不嘗試設置$postclass爲「精選」你的循環之外,並一旦顯示,將其設置爲「常規」?

使不改變邏輯的微小變化對你的代碼:

<?php 
query_posts('cat=5'); 
$postclass = 'featured'; 
while (have_posts()) : the_post(); 

    if (is_paged()) : 
     $postclass = 'regular'; 
    else : 
     echo $postclass; 
     $postclass = 'regular'; 
    endif; 
endwhile 
?> 

或者如果沒有對你有意義,有關使用布爾怎麼樣?

<?php 
query_posts('cat=5'); 
$first = true; 
while (have_posts()) : the_post(); 

    if (is_paged()) : 
     $postclass = 'regular'; 
    else : 
     $postclass = $first ? 'featured' : 'regular'; 
     echo $postclass; 
     $first = false; 
    endif; 
endwhile 
?>