Display only One Single View Post in WordPress

Hey everyone,

I’ve come across a problem when building out a template where we need to display one post one the home page, but it needs to be a single view (so that it displays comments on that post) but cannot impact the query that shows a list of blog posts below. So, here’s a solution:

<?php $wp_query->is_single = true; ?>

    <?php $post_count = 0; ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>    

        <?php if ($post_count == 0) : ?>

            <?php the_content(''); ?>

        <?php $post_count++; ?>

    <?php endif; ?>

<?php endwhile; endif; ?>

Below is a wireframe pic

Hope that helps – let me know if you have any questions.

Amendment:

I left something out, in order to display the rest of the posts I had to output an additional query. Something like the below code should work:

<?php $additional_posts - new WP_Query('cat=1'); ?>
<?php if ($additional_posts->posts) : ?>
<?php while ($additional_posts->have_posts()) : $additional_posts->the_post();  ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This entry was posted in Development, SilentGap, Web and tagged , , . Bookmark the permalink.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

One Response to Display only One Single View Post in WordPress

  1. Jon R says:

    Great post thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>