I would like to implement something like "recent posts" in a static page:

http://themes.codehunk.me/insignio/ (at the footer)

How I would be able to do this without a widget?

link|improve this question

48% accept rate
feedback

2 Answers

Wordpress provides a function for that kind of request: query_posts().

query_posts() is the easiest way to alter the default query that WordPress uses to display posts. Use query_posts() to display different posts than those that would normally show up at a specific URL.

For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don't care about pagination), you can use query_posts() like so:

query_posts( 'posts_per_page=5' );

Once you've performed the query, you can display the posts the way you want.

link|improve this answer
feedback

It kind of depends on what you're going for. If you want to do a "page of posts" -- other words, create a new page template file -- you can create a secondary loop on that page.

The codex has an example of this and here's another, very stripped down example.

<?php
/*
Template Name: Page of Posts
*/
get_header(); 
?>

<?php while( have_posts() ): the_post(); /* start main loop */ ?>

    <h1><?php the_title(); ?></h1>

    <?php
        /* Start Secondary Loop */
        $other_posts = new WP_Query( /*maybe some args here? */ );
        while( $others_posts->have_posts() ): $other_posts->the_post(); 
    ?>
        You can do anything you would in the main loop here and it will
        apply to the secondary loop's posts
    <?php 
        endwhile; /* end secondary loop */ 
        wp_reset_postdata(); /* Restore the original queried page to the $post variable */
    ?>

<?php endwhile; /* End the main loop */ ?>

If you're looking for something that you can drop into any page, the best solution would be a shortcode. You would need to create a shortcode that fetches several posts and returns them in a list (or whatever you want). An example:

<?php
add_action( 'init', 'wpse36453_register_shortcode' );
/**
 * Registers the shortcode with add_shortcode so WP knows about it.
 */
function wpse36453_register_shortcode()
{
    add_shortcode( 'wpse36453_posts', 'wpse36453_shortcode_cb' );
}

/**
 * The call back function for the shortcode. Returns our list of posts.
 */
function wpse36453_shortcode_cb( $args )
{
    // get the posts
    $posts = get_posts(
        array(
            'numberposts'   => 3
        )
    );

    // No posts? run away!
    if( empty( $posts ) ) return '';

    /**
     * Loop through each post, getting what we need and appending it to 
     * the variable we'll send out
     */ 
    $out = '<ul>';
    foreach( $posts as $post )
    {
        $out .= sprintf( 
            '<li><a href="%s" title="%s">%s</a></li>',
            get_permalink( $post ),
            esc_attr( $post->post_title ),
            esc_html( $post->post_title )
        );
    }
    $out .= '</ul>';
    return $out;
}
link|improve this answer
Can I put this into header.php or should I put it somewhere else? – user385917 Dec 15 '11 at 11:49
The first example can go anywhere in your theme. The second, shortcode, example should go in functions.php – Christopher Davis Dec 15 '11 at 16:35
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.