1

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.

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?

4
  • If you can supply the code why don't you do that?
    – Max Yudin
    Feb 12, 2013 at 9:54
  • I'm trying but it doesnt seem to be showing, ill use paste bin. This is what i have so far - pastebin.com/Qq08Z3Fp
    – user27348
    Feb 12, 2013 at 10:47
  • I've already edited your question and it is waiting for peer review. See Code Formatting.
    – Max Yudin
    Feb 12, 2013 at 10:51
  • Sorry, i probably sound really stupid! What's peer review?
    – user27348
    Feb 12, 2013 at 10:57

3 Answers 3

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>
3
  • Thanks for the reply! As i have a static home page i use a template at the moment called page-home-2.php Can i put the code in there? The difficult part for me is knowing where to insert the code in the template?
    – user27348
    Feb 12, 2013 at 10:10
  • 1
    You can add the code into page-home-2.php. I will edit my answer for you adding a short description where to place this code. Feb 12, 2013 at 10:16
  • Mike could you possibly help me further?
    – user27348
    Feb 12, 2013 at 14:53
0

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.

20
  • Thanks for you help, i have just inserted the first code in to my index.php to make a custom template. I now have recent posts showing but with no links and they a are just a small list above my content. How do i make the posts display like they do on other blog pages like this - completemuscleandfitness.com/index.php/training
    – user27348
    Feb 12, 2013 at 10:31
  • That is totally dependent on your current theme's DOM structure. That is, how the presentation logic is written through PHP and HTML. for instance, you can just copy paste the whole post display logic in your custom loop instead of the echo '<li>' . get_the_title() . '</li>'; statement and remove the wrapping ul to that. Hope that helps. Feb 12, 2013 at 10:40
  • Getting lots of input, really appreciate it. I kind of understand what what your saying. I think this is ultimately where i hit a brick wall. I've had the recent posts showing before but cant seem to get how they are displayed right. I just simply cant get my head around it. if i posted another theme blog template could anyone help from that?
    – user27348
    Feb 12, 2013 at 10:51
  • I am unable to get what you are trying to tell. Can you please elaborate more? Feb 12, 2013 at 10:57
  • ok, i can get the recent posts to show on my static page, but they look a mess. There is probably some code i need to insert in to my custom page so that the posts show in the normal theme format. I just have no idea what code to put in a where, this code could probably come from another theme template? I'm asking that if i post a theme template can you help me place the code where it needs to be?
    – user27348
    Feb 12, 2013 at 11:04
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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