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

  1. NWTD
    Posted 4 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:

    			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; ?>

    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
    Volunteer Moderator
    Posted 4 years ago #

  3. Michael
    Forum Moderator
    Posted 4 years ago #

    try to add wp_reset_postdata(); after the endforeach;


  4. NWTD
    Posted 4 years ago #

    thanks @alchymyth, that worked.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.