2011-05-03 5 views
0

while 루프에서 출력되는 첫 번째 항목과 마지막 항목에 첫 번째 및 마지막 클래스를 추가하는 방법을 알아 보려고합니다. 검색을 통해 찾은 유일한 것은 mysql을 직접 사용하는 것과 관련이 있습니다. Wordpress 루프에서 이것을 사용하고 있습니다. (클래스를 만들려는 함수를 넣었습니다.while 루프의 PHP 첫 번째 및 마지막 클래스

<div id="news-loop"> 
    <h2 class="widget-title">News</h2> 
    <?php 
     // Build query for 
     $wp_news_query_temp = clone $wp_query; 
     $wp_news_query = new WP_Query(); 
     $wp_news_query->query('category_name=News&showposts=3&orderby=date&order=DESC'); 
     $news_counter = 0; 
     // Create posts loop 
     if ($wp_news_query->have_posts()) : while ($wp_news_query->have_posts()) : $wp_news_query->the_post(); ?> 
     <div class="news-entry news-entry-<?php echo $news_counter; ?><?php osu_first_last(); ?>"> 
      <h3 class="entry-title"> 
      <?php the_title(); ?> 
      </h3> 
      <?php twentyten_posted_dateonly(); ?> 
      <?php echo osu_short_excerpt(); ?> 
     </div> <!-- End div.news-entry --> 
     <?php 
     $news_counter++; 
     endwhile; ?> 
     <?php endif; $wp_query = clone $wp_news_query_temp; ?> 
     <a href="<?php bloginfo('url'); ?>/category/news/" class="sidebar-more">View all news</a> 
</div> 

누구나 최선을 다해 조언 해 줄 수 있습니까?

감사합니다,

+0

CSS를 사용하지 않는 이유는 무엇입니까? – Gordon

+0

안녕하세요, Gordon - 불행히도 IE가 이러한 선택기를 지원하지 않는 것 같습니다 : http : // www. quirksmode.org/css/contents.html CSS를 사용하는 것이 더 좋을 것 같습니다! – Osu

답변

0

OU 사용할 수

OSU current_postpost_count 등) (osu_first_last하는 쿼리와 함께 그것을 전달하여 첫 번째와 마지막 포스트를 결정하는

다음

osu_first_last을 구현() like

function osu_first_last($query) 
{ 
    $extraClass = ""; 
    if($query->current_post == 1) 
    { 
     $extraClass .= "first"; 
    } 
    if($query->post_count == $query->current_post) 
    { 
     if($extraClass != "") 
     { 
      // post is first and last 
      $extraClass .= " "; 
     } 
     $extraClass .= "last"; 
    } 
    return $extraClass; 
} 

코드에서 다음과 같이 보입니다.

<div class="news-entry news-entry-<?php echo $news_counter; ?><?php echo osu_first_last($wp_news_query); ?>"> 
+0

감사합니다. 다음은 그 조언에 기반한 함수입니다 :'function osu_first_last ($ total_posts, $ posts_count) { \t if ($ posts_count == 0) { \t \t $ class = 'first'; \t} elseif (($ posts_count + 1) == $ total_posts) { \t \t $ class = 'last'; \t} \t return $ class; }' – Osu

+0

사실, 당신의 기능이 좋아 보이므로, 나는 그걸로 갈 것입니다 - 다시 한번 감사드립니다! – Osu

0

count($wp_news_query->posts)을 사용하면 쿼리에 의한 게시/페이지 반환 수를 알아야합니다. $wp_news_query->current_post을 사용하면 현재 게시물/페이지의 색인을 얻을 수 있습니다 (0부터 시작) ...

관련 문제