2017-01-03 236 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

好吧让我试试 –

相关问题