WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] how to display multiple latest posts from specific categories (5 posts)

  1. keyaspects
    Member
    Posted 3 years ago #

    Hi, I'm trying to display the latest 3 post from a specific category.

    I have the following code so far, but this only displays the most recent post.(just 1)

    I'm yet to create a second category, but when I do I want to also display the 3 most recent posts from this category in the sidebar too.

    <div id="nav">
                <h2>Latest Posts</h2>
    
                <?php query_posts("post_per_page=1"); the_post(); ?>
    
    			<h3><?php the_date(); ?></h3>
    			<h4><?php the_title(); ?></h4>
    			<p><?php the_excerpt(); ?>
    			<br />
    			<a href="<?php the_permalink(); ?>" title="Read More About This News Item">[ Read More ]</a></p>
    
            </div>
    
            <?php wp_reset_query(); ?>
  2. alchymyth
    Forum Moderator
    Posted 3 years ago #

  3. keyaspects
    Member
    Posted 3 years ago #

    I've added the following code which controls the number:

    <?php query_posts('posts_per_page=3'); if (have_posts()) : while (have_posts()) : the_post();?>

    closing the while and if loops

    although interesting what one of the links you gave me said.
    Does this basically do the same thing?

    Show x Posts per page
    
    Display 3 posts per page:
    
    $query = new WP_Query( 'posts_per_page=3' );

    Not sure where to put ` $query = new WP_Query( 'cat=4' );
    ` in the loop though. I've messed around a bit but can't seam to get it to work.. tried putting it before the query_posts and after it, within the while loop.

  4. alchymyth
    Forum Moderator
    Posted 3 years ago #

    the second and third link only refer to the parameters which are the same for WP_Query() and query_posts()

    Not sure where to put $query = new WP_Query( 'cat=4' );

    don't get confused by $query = new WP_Query( 'posts_per_page=3' ); or so.
    simply keep working with query_posts() and use the parameters from the linked refs.

    example of using more than one parameter:
    i.e.

    <?php query_posts('posts_per_page=3&cat=4'); if (have_posts()) : while (have_posts()) : the_post();?>

  5. keyaspects
    Member
    Posted 3 years ago #

    right, got ya - thanks alchymth.
    works perfect now.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.