- Firstly, you don’t need to check with
is_single()
insingle.php
. WordPress will automatically recognize it for single post. - Secondly, breakdown your template with in parts. Such as break your main loop code in
content-single.php
and call it insingle.php
withget_template_part('content', 'single')
.
So you single.php
template will look like below-
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('content', 'single-heading');?>
<?php endwhile; ?>
<?php //sidebar widget goes here ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('content', 'single');?>
<?php endwhile; ?>
And create two files content-single.php
and content-single-heading.php
in the directory where single.php
is. Then put below code in content-single.php
–
<div class="post-content">
<!--Post content and footer goes here-->
</div>
And this piece of code in content-single-heading.php
–
<div class="post-heading-info">
<!--Post heading info like title, category goes here -->
</div>
That will make your theme code more maintainable. And for further information read WordPress codex and analyze WordPress default themes.