Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Stephen McMillan
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Stephen McMillan
iOS Development with Swift Techdegree Graduate 33,994 Points

Getting recent Wordpress posts

Hello,

I was wondering if Wordpress offers something similar to the get recent summary page that Treehouse has on its blog. It's for a project much like the Blog Reader App except using wordpress.

Thanks!

2 Answers

Matt Campbell
Matt Campbell
9,767 Points

You want to get x number of most recent posts? That's just a very basic WP loop. By default it gets the most recents posts from the post type "posts", the standard WP posts that come with any install.

If you've made some custom post types, you'll need to include them with a WP_Query array.

Hey Stephen,

This is strait from the Wordpress Codex but here is the code you need to show recent posts.

within the line which starts with $args - set the number => 5 to however many number of posts you want.

/* Code begins */

<h2>Recent Posts</h2> <ul> <?php $args = array( 'numberposts' => '5' ); $recent_posts = wp_get_recent_posts( $args ); foreach( $recent_posts as $recent ){ echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> '; } ?> </ul>

/* Code Ends */

Cheers