Recent posts on a static home page

I want a static home page on my wordpress website, I have already set this up and have a bit of content on it, the next step is I want to have 5 recent posts underneath the content. As a normal page this is no problem, as soon as I set the page to a static home page, the posts disappear.

From hours of searching I am aware that I need ‘multiple loops’ in my template, I have found a few examples and tried to implement them in my template with no success. I think I pretty much have the code to bring the recent posts through, bust I think I’m having trouble markup and template tags to actually show the posts.

Read More

I hope this isn’t too complicated to understand, and i really appreciate any help, I’ve been at this for 2 days with no progress at all. I can supply any code needed from templates or anything else.

My website – www.completemuscleandfitness.com

Ok, this is what i have so far

<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );

foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
}       

which shows this – www.completemuscleandfitness.com

How can i get it to look nice like the other post pages?

Related posts

Leave a Reply

3 comments

  1. First, Welcome!

    I guess you use the file front-page.php for displaying your homepage.
    What you have to do, is add the following code into your front-page.php:

    <h2>Recent Posts</h2>
    <ul>
    <?php
        $args = array( 'numberposts' => '5' );
        $recent_posts = wp_get_recent_posts( $args );
    
        foreach( $recent_posts as $recent ){
            echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
        }
    ?>
    </ul>
    

    More information about wp_get_recent_posts() can be found here.

    Your template file will look something like the following:

    <div id="main">
        <div class="new_post">
            ...
        </div>                                  
    </div>
    

    You could try putting this code into <div id="main"> right before the closing tag </div>:

    <div id="main">
        <div class="new_post">
            ...
        </div>
        <h2>Recent Posts</h2>
        <ul>
        <?php
            $args = array( 'numberposts' => '5' );
            $recent_posts = wp_get_recent_posts( $args );
    
            foreach( $recent_posts as $recent ){
                echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
            }
        ?>
        </ul>                               
    </div>
    
  2. You can create your own custom homepage template same as index.php in your active theme directory.

    Inside that file you can create your own custom query and iterate through results.

    Ex.

    <h2>Recent Posts</h2>
    <ul>
    <?php
        $query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'DESC' ) );
    
        while ( $query->have_posts() ) :
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    endwhile;
    ?>
    </ul>
    

    This you can think as a sample implementation. You can go further as needed.

    Ex.

        <?php 
    
    // The Query
    $the_query = new WP_Query( $args );
    
    // The Loop
    while ( $the_query->have_posts() ) :
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    endwhile;
    
    /* Restore original Post Data 
     * NB: Because we are using new WP_Query we aren't stomping on the 
     * original $wp_query and it does not need to be reset.
    */
    wp_reset_postdata();
    
    
    /* The 2nd Query (without global var) */
    $query2 = new WP_Query( $args2 );
    
    // The 2nd Loop
    while( $query2->have_posts() ):
        $query2->next_post();
        echo '<li>' . get_the_title( $query2->post->ID ) . '</li>';
    endwhile;
    
    // Restore original Post Data
    wp_reset_postdata();
    
     ?>
    

    You can get more information here. And for multiple parameters of the query, refer this.

  3. Unless I’m missing something, I think you are all working much too hard.

    Seems to me that the “recent posts” widget should be all you need.

    Example:

    http://wordpresstest.hjcs.org

    The “Recent News” is the “recent posts” widget.

    (This site is still under construction–that’s why I have “test” in the sub-domain.)