Display Recent Posts without Plugin

3. Oktober 2008 in WordPress Funktionen 10 Kommentare

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.

,

Simon [webdemar]

Moin, ich bin Simon und betreibe webdemar.com als meine ernstgemeinte Spielwiese für WordPress, Webdesign und so.

RSS abonnieren Twitter

10 Kommentare

  • 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.

  • 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 :)

  • John
    16. Dezember 2008

    Is there a way to exclude the most recent post?

  • webdemar
    16. Dezember 2008

    @John:

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

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

  • Yancilane
    27. Februar 2009

    Thank you very much for this useful tip !

  • Ken
    6. März 2009

    Great article. Smaller than a plugin; Saved page load time.

  • Pontus
    7. Mai 2009

    Why would you want to use a new query, when you simply could do wp_get_archives(‘type=postbypost&limit=10′).

    Way better.

  • Sciabb
    1. Juli 2009

    Where do you exactly place the code $recent = new WP_Query(”showposts=5&offset=1″); to make it work??? Thanks

  • artyom
    4. Juli 2009

    if you need offset function you can use this code
    have_posts()) : $recent->the_post();?>

Kommentar schreiben

Dein Name hier
Dein Name hier
11. March 2010

Gravatars sind an. Jetzt kostenlos registrieren!

WordPress Hosting

Für das Hosten von WordPress Blogs empfehlen wir Webspace von Hostloco.
Eine ganze Fotowelt mit nur einer Software - Fotobuch-Software von CEWE.

So am Rande

WordPress 2.8.6 Security Release - 16. November 2009

WordPress versorgt uns von der nächsten großen Version 2.9 noch mit einem weiteren Sicherheits-Release – WordPress 2.8.6. Damit wird eine [...]

WordPress 2.8.5 schließt Sicherheitslücke - 21. Oktober 2009

Seit heute Nacht ist WordPress 2.8.5 zum Download erhältlich. Obwohl schon alle Maschinen für die Version 2.9 laufen, schließt WordPress [...]

WordPress 2.9 kündigt sich an - 14. Oktober 2009

Bald ist es soweit und die erste Beta-Version von WordPress 2.9 wird zum Download erhältlich sein. Das soll laut Peter [...]

Link: Anleitung zur Erstellung von Theme-Options in einem WordPress-Theme - 5. Oktober 2009

Wer ein professionelles WordPress-Theme erstellen möchte, kommt um Theme-Options, einer separaten Seite für Theme-Einstellungen im WordPress-Admin, nicht herum. Theme-Options bieten [...]

Link: Digging into WordPress - 1. Oktober 2009

Sollte es unter Euch einen WordPress-Begeisterten geben, der diese Seite noch nicht kennt, möchte ich ihm diese hiermit vorstellen. Auf [...]

Kostenloser Mozilla Firefox Download des aktuellen Mozilla Firefox Browser.
Offizielles Stadtportal für München. Ein Service für die Landeshauptstadt München.
Do you like this theme?

Powered by WordPress.org - Copyright © 2009. Alle Rechte vorbehalten - Professional WordPress Themes