2017-01-03 1 views
-1

안녕하세요 여러분, 특정 카테고리의 최근 게시물을 5 개 렌더링 한 코드가 있습니다. 여기에 내가 wated 무엇특정 카테고리의 게시물을 워드 프레스에서 표시하는 방법

function postsbycategory() { 
// the query 
$the_query = new WP_Query(array('category_name' => 'news', 'posts_per_page' => 5)); 

// The Loop 
if ($the_query->have_posts()) { 
    $string .= '<ul class="postsbycategory widget_recent_entries">'; 
    while ($the_query->have_posts()) { 
     $the_query->the_post(); 
      if (has_post_thumbnail()) { 
      $string .= '<li>'; 
      $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array(50, 50)) . get_the_title() .'</a></li>'; 
      } else { 
      // if no featured image is found 
      $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>'; 
      } 
      } 
    } else { 
    // no posts found 
} 
$string .= '</ul>'; 

return $string; 

1에서 5까지 특정 범주에서 위의 코드를 표시 최근 게시물하지만 난 더 나은 understadings이 예에 초점을 들어 2에서 6으로 최근 게시물을 표시하도록 때문이다 나 피자라는 장르가 그 카테고리 I 10 게시물 그렇다면 포스트 1 후 4 포스트 (5) 후 6 후 7 후 8 포스트 9 후 10 게시 2 등록 라는 있다고 가정 나는 5 하지만 난이 후 2 후 3 후 4원함 카테고리 피자에서 게시물을 표시 할 수 있습니다 후 1 후 2 후 3 후 4 게시 할 예정입니다 내 홈페이지에 위의 코드 적용게시물 5 게시물 6

예 코드는 1에서 2가 아닌 게시물을 표시하기 시작합니다. 이렇게하는 방법. 저는 웹 개발자가 아니므로 도와주세요.

답변

1

루프에 카운터 ($ i)를 추가하고 카운터를 사용하여 첫 번째 게시물을 건너 뛰고 처음 6 개의 게시물을 가져옵니다.

function postsbycategory() { 
// the query 
$the_query = new WP_Query(array('category_name' => 'news', 'posts_per_page' => 6)); 

$i=1; 
// The Loop 
if ($the_query->have_posts()) { 
$string .= '<ul class="postsbycategory widget_recent_entries">'; 
while ($the_query->have_posts()) { 
    $the_query->the_post(); 
    if($i>1){ 
     if (has_post_thumbnail()) { 
     $string .= '<li>'; 
     $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array(50, 50)) . get_the_title() .'</a></li>'; 
     } else { 
     // if no featured image is found 
     $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>'; 
     } 
     } 
     ++$i; 
     } 
} else { 
// no posts found 
} 
$string .= '</ul>'; 

return $string; 
} 
+0

죄송 펑크 닥 나는 개발자는 그래서 당신은 당신이 위의하십시오 쓰기 무엇을 나를 위해이 작업을 수행 할 수 있지 않다. –

+1

답변에 게시 된 코드는 요청한대로 작동합니다. –

+0

괜찮아 보자. –

관련 문제