0

I am developing a wordpress theme. In single.php page I want to loop similar to this,

<?php while ( have_posts() ) : the_post(); ?>
<?php if ( is_single() ) : ?>
            <div class="post-heading-info">
               <!--Post heading info like title, category goes here -->
            </div>
<?php  endif; ?>
<?php endwhile; ?>
<?php //sidebar widget goes here  ?>

<?php while ( have_posts() ) : the_post(); ?>
<?php if ( is_single() ) : ?>
            <div class="post-content">
               <!--Post content and footer goes here-->
            </div>
<?php  endif; ?>
<?php endwhile; ?>

So, in the code above I am trying to loop all the basic information like title, category and time. The In the middle I want to put the sidebar and then in the second loop the content and the post's footer information.

I need this because of various options in single.php page layout. Basically I want keep all header info on top above all the widgets and post contents.

My question is, Is this the code above acceptable as a good practice?

Please help me in this and your few minutes from your life for my question would be really appreciated and helpful.

  • you are asking about 'good practice' - does that mean the code is actually working? – Michael Aug 31 '16 at 1:24
0
  1. Firstly, you don't need to check with is_single() in single.php. WordPress will automatically recognize it for single post.
  2. Secondly, breakdown your template with in parts. Such as break your main loop code in content-single.php and call it in single.php with get_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.

0

Yes, this is perfectly normal, though you have some redundant bits.

is_single() check is unnecessary since in this case your template is already in that context.

while( have_posts() ) is pretty much unnecessary too on single views (you only have one post, why loop through it?), though core themes tend to still include it.

  • Thanks you for your help and I will do the best thing for the theme. – Allen Titan Aug 31 '16 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.