Forums

[resolved] Recent Post List in a Page (5 posts)

  1. hakanu
    Member
    Posted 3 years ago #

    Hey there, I've had good experiences working with wordpress, and the community is generally very helpful.

    I want to know how I could make a page that would have its static content on top, and a recent post list beneath that.

    From what I can tell, my best bet would be to use a loop to display the recent post list in a template, but I can't seem to get it to work. I'll end up with the content there, but the posts no where to be found.

    I would much appreciate it if someone could help me with this conundrum.

  2. stvwlf
    Member
    Posted 3 years ago #

    Hi

    This code displays the 5 most recent posts. You put it in a custom page template after the WP loop has closed.
    http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

    In the WP editor, assign your custom template to the static page you created as its template, rather than the default template. (box to the right of the edit window)

    <h3>Recent Posts</h3>
    <ul>
    <?php
        $recentPosts = new WP_Query();
        $recentPosts->query('showposts=5');
    ?>
    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
  3. hakanu
    Member
    Posted 3 years ago #

    Thank you for your swift reply, and you've been very helpful. I've been able to get the template working, but I'm afraid I must have phrased by request poorly.

    You see, I want to list the most recent posts as done in the default front page, with the title and then a bit of content.

    I am very encouraged by my progress so far, but I don't know much php.

    Your help is, again, very much appreciated.

  4. stvwlf
    Member
    Posted 3 years ago #

    Try this code

    <h3>Recent Posts</h3>
    <?php
        $recentPosts = new WP_Query();
        $recentPosts->query('showposts=5');
    ?>
    <?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
        <p>the_excerpt();</p>
    <?php endwhile; ?>
  5. hakanu
    Member
    Posted 3 years ago #

    Thank you for your help, based on this code (and the code from my index.php) I've been able to put together a recent post listing just as I wanted. I now face a different problem with comments, but that seems to be a more general glitch and as such is not to be discussed here. I am marking this topic as resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic