WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. apellicone
    Member
    Posted 1 year 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
    Member
    Posted 1 year ago #

    Try using 'numberposts' instead of 'posts_per_page'.

  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    get_posts() requires a 'foreach' loop:
    http://codex.wordpress.org/Template_Tags/get_posts

    before a default loop, use query_posts():
    http://codex.wordpress.org/Function_Reference/query_posts

    or even better use WP_Query() to build the loop:
    http://codex.wordpress.org/Class_Reference/WP_Query

Topic Closed

This topic has been closed to new replies.

About this Topic