Recent posts on a static home page

Hi guys, first post!

I’ve always used the official Wordpress forum’s for help on my website but i must admit it’s very difficult and most admins and mods just refer you to web pages. A friend has refereed me to this forum as he has had great assistance from here.

So, the problem i’m having…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 im 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 - <snip>

My website link has been removed, i can assure you I’m not posting so that i can advertise my website, genuinely after some help with this.

Thanks in advance.

What I think you need to do is setup a new page template (but I could be wrong on this). It would have the logic of a Page Template and the logic of the Main Index Template.

Something similar to:

<?php get_header(); ?>
        
        <div id="content">
        
	  		<?php if (have_posts()) : ?>

			<?php while (have_posts()) : the_post(); ?>
        	
            <div class="post">
            
            <h1><?php the_title(); ?></h1>
            
			<?php edit_post_link(); ?>
            
            <?php the_content(); ?>
            
            </div>
            
            <?php endwhile; ?>
			<?php else : ?>
    		<p><?php _e('Sorry, but nothing seems to match what you were looking for.'); ?></p>
    		<?php endif; ?>
				
						<?php $query = new WP_Query( array( 'post_type' => 'post', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 5 ) ); ?>
						<?php while ($query->have_posts()) : $query->the_post(); ?>
        	
            <div class="post">
            
            <h1><img src="<?php bloginfo('template_url'); ?>/images/header_left.gif" alt="title pic" />&nbsp;<a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>
            <p class="small"><b>Posted by</b> <?php the_author(); ?> on <?php the_date(); ?></p>
            
            <?php edit_post_link(); ?>
            
<?php if(is_category() || is_archive()) {
     the_excerpt();
 } else {
     the_content();
 } ?>
            
            <div class="postmeta">
            <?php if(function_exists('the_ratings')) { the_ratings(); } ?><br />
<b>Filed Under:</b> <?php the_category(', ') ?>
            <b>Comments:</b> <?php comments_popup_link('Be the First to Comment', 'Read the First Comment', '% Comments to Read', 'comments-link', 'Comments are off for this article'); ?><br />
            
            <?php if ( function_exists('the_tags')) the_tags('<h4>Tags: <span>', ', ', ' </span></h4>'); ?>
            
   		<?php comments_template(); ?>
            </div>
            </div>
            
            <?php endwhile; ?>
            
        </div><!--/content-->

<?php get_sidebar(); ?>
    
<?php get_footer(); ?>

The key lines in the above are

						<?php $query = new WP_Query( array( 'post_type' => 'post', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 5 ) ); ?>
						<?php while ($query->have_posts()) : $query->the_post(); ?>

Those two lines are telling WP to query all posts, ordered by date descending, limiting it to 5 per page. Then the while loop and the_post are being referenced by the $query variable that holds those posts.

Thanks for the reply,

This is what ive managed to get together so far, it shows my posts but its a bit of a mess, text outside lines of the content area and same 3 posts cycle continuously down the page, one even comes up as full post.

Any ideas?


    <?php
    /*
      Template Name: custom home
     */
    ?>
	
	

        <?php
        $et_ptemplate_settings = array();
        $et_ptemplate_settings = maybe_unserialize(get_post_meta($post->ID, 'et_ptemplate_settings', true));

        $fullwidth = isset($et_ptemplate_settings['et_fullwidthpage']) ? (bool) $et_ptemplate_settings['et_fullwidthpage'] : false;

        $et_ptemplate_blogstyle = isset($et_ptemplate_settings['et_ptemplate_blogstyle']) ? (bool) $et_ptemplate_settings['et_ptemplate_blogstyle'] : false;

        $et_ptemplate_showthumb = isset($et_ptemplate_settings['et_ptemplate_showthumb']) ? (bool) $et_ptemplate_settings['et_ptemplate_showthumb'] : false;

        $blog_cats = isset($et_ptemplate_settings['et_ptemplate_blogcats']) ? (array) $et_ptemplate_settings['et_ptemplate_blogcats'] : array();
        $et_ptemplate_blog_perpage = isset($et_ptemplate_settings['et_ptemplate_blog_perpage']) ? (int) $et_ptemplate_settings['et_ptemplate_blog_perpage'] : 10;
        ?>

        <?php get_header(); ?>
        <div id="wrap"<?php if ($fullwidth) echo ' class="no_sidebar"'; ?>>
            <!-- Main Content-->
            <img src="<?php bloginfo('template_directory'); ?>/images/content-top<?php if ($fullwidth) echo ('-full'); ?>.gif" alt="content top" class="content-wrap" />
            <div id="content">
                <!-- Start Main Window -->

                <div id="main">
                    <?php get_template_part('includes/default'); ?>
					<?php
                    $query = new WP_Query(array('orderby' => 'date', 'order' => 'DESC'));

                    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
                            ?>
                            <div class="new_post entry clearfix">

                                <div class="postcontent">
                                    <?php
                                    $width = (int) get_option('polished_thumbnail_width_pages');
                                    $height = (int) get_option('polished_thumbnail_height_pages');
                                    $classtext = 'post_img';
                                    $titletext = get_the_title();

                                    $thumbnail = get_thumbnail($width, $height, $classtext, $titletext, $titletext);
                                    $thumb = $thumbnail["thumb"];
                                    ?>

                                    <?php if ($thumb <> '' && get_option('polished_page_thumbnails') == 'on') { ?>
                                        <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                                    <?php }; ?>
                                    <?php the_content(); ?>
                                    <?php wp_link_pages(array('before' => '<p><strong>' . esc_html__('Pages', 'Polished') . ':</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

                                    <div id="et_pt_blog">
                                        <?php
                                        $cat_query = '';
                                        if (!empty($blog_cats))
                                            $cat_query = '&cat=' . implode(",", $blog_cats);
                                        else
                                            echo '<!-- blog category is not selected -->';
                                        ?>
                                        <?php
                                        $et_paged = is_front_page() ? get_query_var('page') : get_query_var('paged');
                                        ?>
                                        <?php query_posts("showposts=$et_ptemplate_blog_perpage&paged=" . $et_paged . $cat_query); ?>
                                        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

                                                <div class="et_pt_blogentry clearfix">
                                                    <h2 class="et_pt_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

                                                    <p class="et_pt_blogmeta"><?php esc_html_e('Posted', 'Polished'); ?> <?php esc_html_e('by', 'Polished'); ?> <?php the_author_posts_link(); ?> <?php esc_html_e('on', 'Polished'); ?> <?php the_time(get_option('polished_date_format')) ?> <?php esc_html_e('in', 'Polished'); ?> <?php the_category(', ') ?> | <?php comments_popup_link(esc_html__('0 comments', 'Polished'), esc_html__('1 comment', 'Polished'), '% ' . esc_html__('comments', 'Polished')); ?></p>

                                                    <?php
                                                    $thumb = '';
                                                    $width = 184;
                                                    $height = 184;
                                                    $classtext = '';
                                                    $titletext = get_the_title();

                                                    $thumbnail = get_thumbnail($width, $height, $classtext, $titletext, $titletext);
                                                    $thumb = $thumbnail["thumb"];
                                                    ?>

                                                    <?php if ($thumb <> '' && !$et_ptemplate_showthumb) { ?>
                                                        <div class="et_pt_thumb alignleft">
                                                            <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                                                            <a href="<?php the_permalink(); ?>"><span class="overlay"></span></a>
                                                        </div> <!-- end .thumb -->
                                                    <?php }; ?>

                                                    <?php if (!$et_ptemplate_blogstyle) { ?>
                                                        <p><?php truncate_post(550); ?></p>
                                                        <a href="<?php the_permalink(); ?>" class="readmore"><span><?php esc_html_e('read more', 'Polished'); ?></span></a>
                                                    <?php } else { ?>
                                                        <?php
                                                        global $more;
                                                        $more = 0;
                                                        ?>
                                                        <?php the_content(); ?>
                                                    <?php } ?>
                                                </div> <!-- end .et_pt_blogentry -->

                                            <?php endwhile; ?>
                                            <div class="page-nav clearfix">
                                                <?php
                                                if (function_exists('wp_pagenavi')) {
                                                    wp_pagenavi();
                                                } else {
                                                    ?>
                                                    <?php get_template_part('includes/navigation'); ?>
                                                <?php } ?>
                                            </div> <!-- end .entry -->
                                        <?php else : ?>
                                            <?php get_template_part('includes/no-results'); ?>
                                        <?php
                                        endif;
                                        wp_reset_query();
                                        ?>

                                    </div> <!-- end #et_pt_blog -->

                                    <?php edit_post_link(esc_html__('Edit this page', 'Polished')); ?>
                                    <div class="clear"></div>

                                </div> <!-- end .post -->
                            </div>
                            <?php
                        endwhile;
                    endif;
                    wp_reset_postdata();
                    ?>
                </div>
                <!-- End Main -->

                <?php if (!$fullwidth) get_sidebar(); ?>
                <?php get_footer(); ?>