Displaying Recent Pages (Not Posts) With WordPress

I’ve spent some time in the last few days updating the portfolio design for the site. Each item in my portfolio is a seperate WordPress page. I wanted the portfolio index to highlight the most recent portfolio items in a list, and to update this list automatically every time I add a new item.

Picture of old book

Highlighting recent posts in WordPress is really easy. The basic WordPress index.php page does it automatically, adding each new blog post to the page as it’s written. It’s also easy to add links to those posts through sidebar widgets. It’s more difficult to highlight pages based on when they were created. Pages are supposed to be more static, so date based calls to that content is less common.

Making WordPress Look On Pages As Posts

The portfolio page on my site uses a custom template. I worked on some code to make WordPress look at new pages in the same way it looks at posts, and display them based on how recently they were created.

<?php $portfolio_items = array(
 'post_type' => 'page',
 'showposts' => 3
 );
 $posts = get_posts($portfolio_items);
 if ($posts) {
 foreach ($posts as $post) {
 setup_postdata($post);
 } } ?>

This makes WordPress pull down pages as $portfolio_items (this can be defined as anything), which are defined to render in the same was as posts. I can then define how many I want to show.

Displaying The Information On The Page

Now I need to display the information on the page (the above code makes WordPress pull down the correct information, but doesn’t display anything. I can do this by adding code in the same way as I’d display any post or page when developing a WordPress template.

<?php $portfolio_items = array(
 'post_type' => 'page',
 'showposts' => 3
 );
 $posts = get_posts($portfolio_items);
 if ($posts) {
 foreach ($posts as $post) {
 setup_postdata($post);
?>

<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Link to
<?php the_title() ; ?> portfolio page"><?php the_title(); ?></a></li>

<?php } } ?>

I just want to display the three most recent pages as links in a list, but you could display excerpts from the page, full articles or any other information if you wanted.

What If The Page Isn’t A Portfolio Piece?

We’re nearly finished, but there’s one small flaw. If all I ever added to my site was portfolio item pages then there’d be no problem. The code is pulling down any new page, though, so if I add a new page that’s not a portfolio piece it will still show up. I need to limit my results to a subset of pages.

Luckily WordPress makes this easy. All my portfolio item pages are children of the main portfolio page. So I simply need to add a parameter that limits the pages that are getting called.

I need to add a new line:

'post-type' => 'page',
'post_parent' => '163', // this is the new line
'showposts' => 3

‘163′ is the page ID of my portfolio page. Obviously you would need to check on the ID of your own pages if you were implementing this on your own site.

I’m happy with the results. The portfolio page calls a simple list of my three most recent portfolio pieces, and will update that list automatically every time I add a new one.



One Comment

  1. Posted May 30, 2010 at 12:44 pm | Permalink

    Thank you! I was looking for this exact code and couldn’t find it til I came across your site. It worked perfectly.

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>