Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

88 PART II • Designing and Developing WordPress Themes


// Comments
comments_template( '', true );

// Loop ends
endwhile;
?>

</section> <!-- #main-container ends -->

<?php get_sidebar(); ?>

</div>

<?php get_footer(); ?>

Thanks to get_template_part(), single.php will try to load content-single.php first,
and failing that, it’ll load content.php. Create a content-single.php file as well, based on the
original content.php but without a linked title, using h1 instead of h2 and no check whether
it is a single post or page either (because you already know that). Here it is:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header>
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="entry-meta">
Posted on <time datetime="<?php echo get_the_date(); ?> <?php
the_time(); ?>"><?php the_date(); ?> <?php the_time(); ?></time>
by <?php the_author_link(); ?>
<?php
// Are the comments open?
if ( comments_open() ) : ?>
• <?php comments_popup_link( 'No comments', '1 comment', '%
comments' ); ?>
<?php endif; ?>
</p>
</header>
</article>

Actually, besides the preceding changes, which I’m sure you’ll have no trouble finding if you
compare this to content.php, I did add an additional post meta line that lists the category
separated by commas. If there are any tags for the post, those are also added after the category.
Having this in listings is sometimes preferred, but here it serves as an example of when a
separate single.php template file is warranted.

Moving on to page.php, you will see that it is very similar. In fact, you could just copy the
contents from single.php, paste it into page.php, and be done with it. Well almost, because
page.php doesn’t have categories or tags. Fix that in content-single.php because that is now
used on page.php. If you wanted a different look and feel for your pages, you would probably
create a content-page.php file and use get_template_part( 'content', 'page' )
Free download pdf