Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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".

share|improve this question
add comment

1 Answer 1

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);
share|improve this answer
    
Ah, thanks. I'll try that :) –  Steven Jan 2 '12 at 9:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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