• Resolved skeeterz71

    (@skeeterz71)


    Hello,

    I am wanting to display the titles of 3 recent posts which excluding the most recent. I currently use this, but it displays the most recent post. How do I alter this or is there a better function for what I want to accomplish.

    `<?php wp_get_archives( array( ‘type’ => ‘postbypost’, ‘limit’ => 3, ‘format’ => ‘html’ ) ); ?>’

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with get_posts().
    http://codex.wordpress.org/Template_Tags/get_posts

    <?php
    $args = array( 'posts_per_page' => 3, 'offset' => 1 );
    $posts = get_posts( $args );
    
    if ( $posts ) {
    	echo '<ul>';
    	foreach ( $posts as $post ) {
    
    		echo '<li><a href="' . esc_url( get_permalink( $post->ID ) ). '">';
    		echo get_the_title( $post->ID ) . '</a></li>';
    	}
    	echo '</ul>';
    	wp_reset_postdata();
    }
    ?>

    Thread Starter skeeterz71

    (@skeeterz71)

    Thanks. After going about 4 levels deep on google after 10 or so searches I finally found it. This will pull 3 posts except for the most recent. That is what the offset does. This includes the title and link. That is all I wanted in the display.

    <h3>Recent Posts</h3>
    
      <ul>
      <?php query_posts('posts_per_page=3&offset=1'); ?>
    	<?php while (have_posts()) : the_post(); ?>
    			<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
    	<?php endwhile; ?>
      </ul>
    Moderator keesiemeijer

    (@keesiemeijer)

    You shouldn’t use query_posts() as it’s meant for the main query. Did you try my example? It’s basically the same as yours without using query_posts.

    http://wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to limit query to recent piosts excluding the most recent’ is closed to new replies.