I'm trying to get posts from 3 different categories and list them on the front page:

  // Carousel articles
  $args = array( 'numberposts' => 3, 'orderby' => 'date', 'category' => 'karusell' );
  $carousel = get_posts($args);

  // News articles
  $args = array( 'numberposts' => 3, 'orderby' => 'date', 'category' => 'nyheter' );
  $news = get_posts($args);

  // Featured articles - max 2 posts
  $args = array( 'numberposts' => 2, 'orderby' => 'date', 'category' => 'feature' );
  $featured_posts = get_posts($args);

Then I output them by doing this:

foreach( $carousel as $post ) : setup_postdata($post);
 // code here
endforeach;

My problem is that all my loops outputs the same posts. Am I using the wrong function for getting posts?

I could use something like this:

query_posts("category_name=feature&posts_per_page=2&orderby=date");
while (have_posts()) : the_post();
//code
endwhile;

But I was hoping I could fetch all the articles at the top of my code and not "inline".

link|improve this question

71% accept rate
feedback

1 Answer

up vote 0 down vote accepted

With get_posts(), try using the category ID instead of the category slug.

For example:

// Carousel articles
$args = array( 'numberposts' => 3, 'orderby' => 'date', 'category' => 1 );
$carousel = get_posts($args);

// News articles  
$args = array( 'numberposts' => 3, 'orderby' => 'date', 'category' => 2 );
$news = get_posts($args);

// Featured articles - max 2 posts
$args = array( 'numberposts' => 2, 'orderby' => 'date', 'category' => 3 );
$featured_posts = get_posts($args);
link|improve this answer
Ah, thanks. I'll try that :) – Steven Jan 2 at 9:29
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.