Do I need to use The Loop on pages?

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?

2 s
2

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.

Leave a Comment