WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display list of recent posts on page (4 posts)

  1. NWTD
    Member
    Posted 3 years ago #

    I'm trying to list the 10 most recent posts on a particular page.

    I'm using the TwentyEleven theme and using the following code:

    <ul>
    		<?php
    			global $post;
    			$args = array( 'numberposts' => 10 );
    			$myposts = get_posts( $args );
    			foreach( $myposts as $post ) :	setup_postdata($post); ?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php endforeach; ?>
    	</ul>

    When I do this, I loose all the content on the page. It is replace by the content of the last post in the list.

    I assume there is a quick simple fix for this to reset the wp_query or something...I'm just not sure.

    Any help would be greatly appreciated. TIA

  2. Tara
    Member
    Posted 3 years ago #

  3. alchymyth
    Forum Moderator
    Posted 3 years ago #

    try to add wp_reset_postdata(); after the endforeach;

    http://codex.wordpress.org/Function_Reference/wp_reset_postdata

  4. NWTD
    Member
    Posted 3 years ago #

    thanks @alchymyth, that worked.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.