Display Recent Posts without Plugin

Display Recent Posts without Plugin

On many WordPress themes you see a section (often in the sidebar) where recents posts are displayed. This is useful both for users and search engines. Users get an overview about the newest articles and find them easy without scrolling the whole blog page. Spiders of search engines get a delicious list with all the permalinks of the latest articles and you make it easier for them to spider your website.

There are plenty of plugins and widgets helping you to display a special number of the latest posts of your blog. But if you’re looking for a simple method to display a list with x recent posts, you don’t need a plugin (we should try to use as few plugins as possible) or a widget. Put it right into your WordPress theme wherever you want… in the sidebar, below the loop in the single post view, in the footer etc.:

<ul>
  <?php $recent = new WP_Query("showposts=5"); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
  <?php endwhile; ?>
</ul>

In this example the output would be an unordered list of the permalinks of the latest 5 posts no matter of which category.

If you want to display only the 3 latest posts of category 6, add the following to your WordPress theme:

<ul>
  <?php $recent = new WP_Query("showposts=3&cat=6"); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
  <?php endwhile; ?>
</ul>

You see how simple it is? ;-) Let’s try to make your WordPress theme more powerful and use less plugins.

Get more useful WordPress Tips.

Tags: ,

Ähnliche Artikel

Hat Dir der Artikel gefallen?

4 Kommentare zu Display Recent Posts without Plugin

webdemar
16. Dezember 2008

@John:

In this case you could use the offset parameter like this:

$recent = new WP_Query(”showposts=5&offset=1″);

John
16. Dezember 2008

Is there a way to exclude the most recent post?

GorillaDude
10. November 2008

Hi, This is a very useful post, but the font used on your site makes it really hard to read. A friendly suggestion would be to make the text color easier to read :)

Matt Gagnon
20. Oktober 2008

Thanks for that quick tip. I really think that I use too many plugins and that I want to cut back on how many resources that I use.

Sag Deine Meinung!

custom-wordpress-theme

Alex sagt zu Superfish jQuery Menu IE z-index Bug:
Nice one! I read the klog post before reading this and somehow I had missed...

webdemar sagt zu Frohes Neues!:
@Kristin: Vielen Dank ;-)...

Kristin sagt zu Frohes Neues!:
Ich wünsche dir auch ein frohes neues Jahr. Ich wünche dir viel Glück bei d...

Alec sagt zu WordPress Theme deFusion 0.9:
Really like your deFusion theme. I've tinkered with it, and thought you mig...

Ahmet Topal sagt zu Sticky Posts in WordPress 2.7:
Ich glaube, dass war Otto42, der dass mit dem Query gesagt hat :?...