如何仅显示前两个帖子?

时间:2016-01-26 12:30:55

标签: php wordpress

我不再使用WordPress主题开发了。我试图只在我的主页上显示最近的两个帖子,但无论我做什么,代码总是遍历整个帖子。任何帮助表示赞赏。

的index.php:

cboProductType.ItemsSource = e.Result;
cboProductType.Items.Insert(0, "--Select Products--");  //error on this line

3 个答案:

答案 0 :(得分:1)

使用此

$args = array ( 
          'order' => 'DESC',
          'post_type' => 'post',
          'post_status' => 'draft, publish, future, pending, private',
          'posts_per_page' => 2,
          'orderby' => 'post_date',
        );
$query = new WP_Query($args);
if ( $query->have_posts() ) : 
  while ( $query->have_posts() ) : $query->the_post();
    /* Your Code */
  endwhile; 
endif ;

答案 1 :(得分:0)

你必须遍历 wp_get_recent_posts 的返回值:

foreach($recent_posts as $recent_post)  {
  // stuff
}

此外,在 args 中设置numberpost为2。

答案 2 :(得分:0)

将第3行更改为:

'numberposts' => 2,

以下是该功能的完整文档:https://codex.wordpress.org/Function_Reference/wp_get_recent_posts