Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm writing my first WordPress theme and have a question about the use of The Loop in page templates. The page templates I've looked at all follow basically the same pattern (example from Twenty Twelve):

<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>

But a page will only have one post associated with it so iterating through the posts seems unnecessary. My page template is more readable and seems to work fine:

<?php
the_post();
the_title('<h1>', '</h1>');
the_content();
?>

Is this a good practice? Are there any downsides?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

According to the Theme Guide, full loops should be used, even on single templates.

Full loops must be used in all templates. Just calling the_post() in a template like single.php or page.php is not enough.

So yes, it's a best practice to use full loops.

share|improve this answer
add comment

The only potential (edit: functional) issue I see is that the loop_end action won't get called if you don't call have_posts() after the last the_post(), so anything hooked to that action won't run.

In practice, I've been doing it that way in bespoke client themes and have never encountered an issue, but it depends on what plugins may be used and whether or not they hook anything on loop_end.

share|improve this answer
    
According to the Theme Guide, full loops should be used, even on single templates. "Full loops must be used in all templates. Just calling the_post() in a template like single.php or page.php is not enough." So yes, it's a best practice to use full loops. –  goto10 Feb 17 '13 at 16:56
    
you should probably add that as an answer. it's necessary if you want your theme accepted and distributed on WP.com or org. –  Milo Feb 17 '13 at 17:06
    
Alrighty, thanks. I will. I thought you covered it with your answer, so I thought I'd just supplement it with that tidbit. I had never thought about the loop_end action point that you brought up. Good call there. –  goto10 Feb 17 '13 at 17:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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