2017-01-03 234 views
-1

嘿,我有一個代碼,它呈現了來自特定類別的5個最近的帖子。下面是如何在wordpress中顯示來自特定類別的帖子

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; 

我wated那是什麼上面的代碼顯示最近從特​​定的類別從1到5的職位,但我想顯示最近帖子2至6。爲了更好地understadings關注這一例如 假設我有一個名爲披薩類和在該類別中我有10個帖命名 張貼1 郵政2 郵政3 郵政4 郵政5 郵政6 郵政7 發佈8 郵政9 郵政10 所以,如果我上面的代碼應用到我的主頁將顯示從比薩類崗位是 後1 後2 後3 後4 後5 ,但我想這 後2 後3 後4 後5 後6

是我想要的代碼開始顯示文章2而不是從1那麼,如何做到這一點。 請幫我我不是一個Web開發人員。

回答

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

對不起Funk Doc我不是開發者,所以你可以爲我做這個,你上面寫的是什麼。 –

+1

答案中的發佈代碼將按照您的要求工作。 –

+0

好吧讓我試試 –

相關問題