Print This Post Print This Post
Jun 04

My next modification to many of the templates I’ll use is to show a list of the latest posts made.

I like having this “summary” available to my visitors – makes life a bit easier for them – and on a busy site, it’s a “what’s new” at a glance.

It also makes it easier for search engine spiders to grab your content too.

So how hard is it to add?

Dead simple!

Most templates do NOT include the php code for this, but all you have to do is to:

  • open your sidebar.php file in PRESENTATION/THEME EDITOR

  • work out where you want your latest posts to appear – I like them at/near the top of the sidebar

  • find some existing code in your sidebar which you can modify for this section – COPY and PASTE it where you want it…

    FOR EXAMPLE, in the WP Default theme sidebar, find this code:
    <li><h2>Archives</h2>
    <ul>
    <?php wp_get_archives('type=monthly'); ?>
    </ul>
    </li>

    then copy and paste it immediately above itself (or wherever you want).

  • Change the heading to say “Latest Posts” or whatever…
    e.g. <li><h2>Latest Posts</h2>
    and then
  • REPLACE the old piece of php code you don’t want – i.e.
    <?php wp_get_archives('type=monthly'); ?>
    to this:
    <?php get_archives('postbypost','10'); ?>

And then SAVE the changes and check it out.

Easy Peasy :)

You should see the latest 10 posts made appear in your sidebar. Change the number in ('postbypost','10') to whatever you want – maybe 5, or perhaps 20?

I have NOT actually included this change to my default WP theme file – thought I’d keep that one pretty much to the original.

But I have added it to the iTheme theme. That was not quite as simple as the process described above (the layout of those sidebar boxes is only a little more complicated) but still easy enough with the cut and paste…

Play. Experiment. Learn. Enjoy!

If you like this post, buy me a coffee

Popularity: 20% [?]

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • Furl
  • Netscape
  • NewsVine
  • Reddit
  • scuttle
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

2 Responses to “Latest Posts Code : postbypost”

  1. john ladner Says:

    that’s great, but how do u change the sidebar to random posts titles? who cares if the sidebar dislays the first few posts titles, there’re already displayed anyway.

  2. Stephen Says:

    Hi John

    You’d actually need this plugin from coffee2code to do that:
    http://www.coffee2code.com/archives/2004/08/27/plugin-customizable-post-listings/

    Cheers
    Stephen

Leave a Reply