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”.
With
get_posts()
, try using the category ID instead of the category slug.For example:
Here is another possible solution :