Trying to show posts only in certain category using get_posts but having trouble (3 posts)

  1. apellicone
    Posted 3 years ago #

    I'm trying to build a custom page template that will show only pages within a certain category. I've been following the get_posts guide on the codex, and have this:

    <?php bp_dtheme_content_nav( 'nav-above' ); ?>
    <?php $insp = array('cat' => 4, 'posts_per_page' => 5, 'offset' => 0, 'orderby' => 'post_date');
    $posts = get_posts($insp); ?>
    <?php if ( have_posts() ) : ?>
    <?php while (have_posts()) : the_post(); ?> ... etc, etc,

    This succeeds in limiting it to just posts in category 4, but only shows a single post in the loop. Does anyone know what I'm doing wrong with this code?

    Everything except for the $insp array and the $posts = get_posts is from the default index.php file of my parent theme. Is this in the wrong place? Am I going about this in an ass-backwards way?

  2. vtxyzzy
    Posted 3 years ago #

    Try using 'numberposts' instead of 'posts_per_page'.

  3. Michael
    Forum Moderator
    Posted 3 years ago #

    get_posts() requires a 'foreach' loop:

    before a default loop, use query_posts():

    or even better use WP_Query() to build the loop:

Topic Closed

This topic has been closed to new replies.

About this Topic