Bookmark and Share

Customizing the WordPress Loop for the Single Post

Friday, October 9th, 2009 by MikeLT

While the home page of a blog is generally meant to show off a number of the most recent posts, each actual blog entry, or post, is stored in its own table in the database, and when retrieved resides on its own page.

Each post page is the actual content, and thus the heart of the blog, and is where interactive discussions originate in the form of the comment form. Comments must be turned on in the Admin discussion panel.

In the Template Hierarchy, when a user clicks on a link to a post, WordPress will first look to load the template page “single.php”. If single.php is not found in the themes directory, it will use the index template file.

Let’s examine the code for the single post page loop:

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

<div <?php post_class() ?> id=”post-<?php the_ID(); ?>”>
<h2><?php the_title(); ?></h2>
<small><?php the_time(‘l, F jS, Y’); ?> by <?php the_author(‘,’); ?></small>

<?php the_content(); ?>

<div class=”center>

<?php the_tags( ‘<p>Tags: ‘, ‘, ‘, ‘</p>’); ?>

</div>

<div class=”navigation”>

<?php previous_post_link(‘&laquo; %link’) ?> | <?php next_post_link(‘%link &raquo;’) ?>

</div>

<?php comments_template(); ?>

<!– Post Meta Data –>
<p><small>
You can follow any responses to this entry through the <?php post_comments_feed_link(‘RSS 2.0′); ?> feed.<br />
<?php if ((‘open’ == $post-> comment_status) && (‘open’ == $post->ping_status)) {
// Both Comments and Pings are open ?>
You can <a href=”#respond”>leave a response</a>, or <a href=”<?php trackback_url(); ?>” rel=”trackback”>trackback</a> from your own site.
<?php } elseif (!(‘open’ == $post-> comment_status) && (‘open’ == $post->ping_status)) {
// Only Pings are Open ?>
Responses are currently closed, but you can <a href=”<?php trackback_url(); ?> ” rel=”trackback”>trackback</a> from your own site.
<?php } elseif ((‘open’ == $post-> comment_status) && !(‘open’ == $post->ping_status)) {
// Comments are open, Pings are not ?>
You can skip to the end and leave a response. Pinging is currently not allowed.

<?php } elseif (!(‘open’ == $post-> comment_status) && !(‘open’ == $post->ping_status)) {
// Neither Comments, nor Pings are open ?>
Both comments and pings are currently closed.
<?php } edit_post_link(‘<br />Edit this entry’,”,’.'); ?>

</small></p></div>

<?php endwhile; else: ?>

<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn’t here.</p>

<?php get_search_form(); ?>

<?php endif; ?>

With comments turned on all that is needed to reference the form is the template tag. As you can see, the Post Meta Data becomes a bit more complicated.

At the top next to the post date I added the author tag in case somebody besides me ever wants to post anything here.

After the content I wanted to display the posts tags, and I thought it would be neat if a user could navigate to the previous and next chronological post page from the end of the article.

Bookmark and Share

3 Responses to “Customizing the WordPress Loop for the Single Post”

  1. manny says:

    oh my god, after hours and hours of looking i finally found the file that is responsible for individual posts!
    Thank you so much for pointing out the single.php file!!!!!!!!

  2. [...] Customizing the WordPress Loop for the Single Post [...]

Leave a Reply

You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Host Gator hosting service provider - unlimited domains