The next template we need to create is the single post view. Right now, the single post looks like the site's front page (because it's using index.php)—except it populates the page with the contents of just one post.
To get started, make another copy of index.php, and name it single.php. This is the template that WordPress will look for when serving a single post. If it doesn't find single.php, it'll use index.php.
Without further delay, here's my single.php file. You should notice that the file features almost exactly the same elements as index.php. The only difference is that the get_template_part() function call fetches a different element. In this case, it's single, as demonstrated in the following code:
<?php get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php while (have_posts()) : the_post(); ?> <?php get_template_part('content', 'single'); ?> <?php daily_cooking_custom_post_nav(); ?> <?php if (comments_open() || get_comments_number()) comments_template(); ?> <?php endwhile; // end of the loop. ?> </main><! - #main - > </div><! - #primary - > <?php get_sidebar(); ?> <?php get_footer(); ?>
The aforementioned get_template_part('content', 'single') call will fetch the content-single.php file. Here's what the file looks like:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <?php the_title('<h1 class="entry-title">', '</h1>'); ?> <div class="entry-meta"> <?php daily_cooking_custom_posted_on(); ?> </div> </header> <div class="entry-content"> <?php the_content(); ?> <?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'daily-cooking-custom' ), 'after' => '</div>', ) ); ?> </div> <footer class="entry-footer"> <?php daily_cooking_custom_entry_footer(); ?> </footer> </article><! - #post-## - >
This file's structure is almost exactly the same as the one we discussed a couple of pages ago—content.php. The following are three specific things that are worth pointing out:
- The presence of the <article> tag. The individual post's content is displayed inside this tag.
- The call to the the_content() function. This time, we're displaying the whole content of the post, not just an excerpt.
- The call to the comments_template() function in single.php. This displays the comment form and the individual comments that have been submitted for this post.