Static Front-Page Excerpts

Working on a new site, I’m trying to plant recent post excerpts on a static front-page. I had been doing all my static content outside of wordpress (as static files), but was convinced to pull everything into the WP fold, so to speak.

On my external static file that was serving as my home page, I had figured out how to include the blog header <?php require('../wordpress/wp-blog-header.php'); ?> and then call up posts on that page, and it worked like a charm:

Read More
<?php 
                        $count = 0; 
                    ?>
                    <?php if ( have_posts() ) while ( have_posts() && $count <= 6 ) : the_post(); ?>
                            <section class="post">
                                <header class="post">
                                    <h2><a class="light" href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                                        <?php twentyten_posted_on(); ?>
                                    <div class="hr"></div>
                                </header>
                                <article class="post">
                                    <?php the_excerpt(); ?>
                                </article>
                                <footer class="post">
                                    <?php twentyten_posted_in(); ?>
                                </footer>
                            </section>
                    <?php $count++; ?>
                    <?php endwhile; // end of the loop. ?>

So, now, I have set up a wordpress front-page template, and it is being delivered dynamically, using the my theme header and this same chunk of the loop, I am getting not posts, but pages back. Instead of seeing a set of current posts, I get back some title info for the 3 wp pages currently on my site. I get that this is due to url query variables, but I’m not exactly clear how to get arround it.

I tried manually calling up a new WP-Query(); but that returned nothing at all. Tho I suspect that it may work, given the proper arguments…

Any thoughts on how to pull in recent posts, instead of a list of pages, to a static front-page with this modified loop?

Much appreciated –

Related posts

Leave a Reply

2 comments

  1. Is there any particular reason not to use the front-page.php template file, and just not bother with trying to pull WordPress headers into external files?

    Using the front-page.php template file is easy:

    1. Create a new static Page, with any arbitrary name (let’s call it “Front Page”)
    2. Go to Dashboard -> Settings -> Reading and set “Front Page Displays” to “Static Page”
    3. Set the “Front Page” dropdown to the “Front Page” static Page you created in step 1.
    4. Create a new template file for your theme, called front-page.php, and include in it your custom Loop code, above.

    Also, you should properly filter the main Loop query as output on front-page.php, rather than using the && $count <= 6 conditional in your call to the Loop.

    I would recommend using a simple call to get_posts(), and set the numberposts argument to 6.

  2. Ah – got it – was almost there with the new WP_Query, but was failing to call the query method on it… This works perfectly:

    <?php 
                                $excerptQuery = new WP_Query();
                                $excerptQuery->query('showposts=6');
                            ?>
                            <?php if ( $excerptQuery->have_posts() ) while ( $excerptQuery->have_posts()) : $excerptQuery->the_post(); ?>
                                    <section class="post">
                                        <header class="post">
                                            <h2><a class="light" href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                                                <?php twentyten_posted_on(); ?>
                                            <div class="hr"></div>
                                        </header>
                                        <article class="post">
                                            <?php the_excerpt(); ?>
                                        </article>
                                        <footer class="post">
                                            <?php twentyten_posted_in(); ?>
                                        </footer>
                                    </section>
                            <?php endwhile; // end of the loop. ?>