Lesson 7: Single.php: the dedicated template Wordpress



Displaying a single ticket template requires a richer than others.

Single.php template used to display a single Post. It can be considered as the last in the hierarchy of Posts:

Home home.php (with list of the latest Posts)
archive page archive.php or more precisely:
    category category.php
    year, month, or day date.php
    keyword tag.php
    search results search.php
page dedicated single.php

A slightly different loop

Because it plays a specific role, single.php template will have a different loop. We will not use the include loop.php but copying and pasting and editing.

Single.php The code looks like this:

<?php get_header(); ?>
 <div class="main single"> 
<?php if (have_posts()) : ?>
 <?php while (have_posts()) : the_post(); ?> 
<div class="post"> 
<h1 class="post-title">
<?php the_title(); ?></h1>
 <p class="post-info"> 
Posted  <?php the_date(); ?> in <?php the_category(', '); ?> by <?php the_author(); ?>. 
</p> 
<div class="post-content"> 
<?php the_content(); ?> </div> 
<div class="post-comments"> 
<?php comments_template(); ?> 
</div> 
</div> 
<?php endwhile; ?>
 <?php endif; ?> 
</div> 
<?php get_sidebar(); ?>
 <?php get_footer(); ?>

We find the div "hand" that can serve as main column. I added the class "single" that will allow us to differentiate the style of the post title, post info, post content ... if necessary.

The loop is not called via the include loop.php but is directly integrated with single.php:

post-title loses its connection <a> since it is already on the dedicated page
it is also an <h1> because it is the title of the page you are. With that of the header.php, so it made two <h1>. It will therefore modify the header accordingly. We'll do that later.
post-excerpt becomes a post-content, and function is modified as: is now called the_content ();
a template (which is here a include) comments!
we turned the "else" part that does not serve much as it always has a post there (otherwise we would not be able to have a link to it)
This is the only time we will see the content of a Post appears in full.

Discussion, comments
The comments_template () function generates:

title Style One Response to "Hello world! »
a list of comments, where everyone has:
     an avatar
     an author (with link if supplied)
     a date (which serves permalink)
     content
     a link «Reply / Reply" to reply to a comment rather than Article
     an "Edit" link to admin
title "Post Reply"
a form to post a comment

Lot for a single function, is not it?
What you should know is that this template comment is integrated with WordPress. Indeed, it is not necessary to create a template / include comments.php for this function to display something. Nevertheless free to create one yourself, but I do not recommend it. The comments.php file is a gas plant.

Before WordPress 3 comments.php contained not only the entire code from the list of comments but also the entire code of the form. I had "cleaned" and improved to make it more readable, easier to modify and correct at the HTML level.
Since WordPress 3, I prefer to leave the base template that generates a globally correct code.

If single.php template is dedicated to the display of a single Post, page.php is the one dedicated to the display of a page. So, their similarity seems obvious. But it will still go through two templates and not just because WordPress and works for Posts single.phppage.php for Pages, and no way to merge them.

At the same time, Page has different information to display:

removing the category (since it does not)
the date is removed (because it has little interest for static content, timeless)
Comments are removed (you can keep them but rarely leave them on a page)
In sum, taken up single.php code and removes it in pieces to create page.php:

<?php get_header(); ?>
 <div class="main page">
 <?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?>
 <div class="post">
 <h1 class="post-title">
<?php the_title(); ?>
</h1> 
<div class="post-content"> 
<?php the_content(); ?> 
</div> 
</div> 
<?php endwhile; ?>
 <?php endif; ?>
 </div> 
<?php get_sidebar(); ?> 
<?php get_footer(); ?>

The "single" class becomes a "page" class and the deletion of the parties' post-info "and" post comments ".
Previous
Next Post »