Trying to only display 1 category using get_posts

I’m trying to only display posts from a certain category in a widget. I’ve tried it in a few different ways but it is still showing posts from all categories. Here’s my code at the moment:

$posts = get_posts(array ('category'=>6, 'numberposts'=>$number, 'order'=>'DESC', 'orderby'=>'date'));
        foreach ($posts as $post){
            setup_postdata($post);....

Thanks.

Related posts

Leave a Reply

2 comments

  1. Did you try the example given in the codex?

    <?php
    global $post;
    $args = array( 'numberposts' => $number, 'category' => 6, 'order' => 'ASC', 'orderby' => 'post_date'  );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    

    This a modified version that should suit your needs. Are you also certain that this category exists?

    Edit:

    <?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query('showposts=' . $numberposts . '&cat=6&orderby=date&order=ASC'); ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
            <!-- your content -->
    <?php endwhile; ?>
    <?php $wp_query = null; $wp_query = $temp; ?>