Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Adding a Blog to a WordPress Theme The single.php Page

Jean Bosio
Jean Bosio
1,048 Points

Why does the single.php require the loop?

I noticed the loop is in the single.php code:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

But I don't understand the logic of why it is here since, we only want to insert the content from one particular post?

Thanks.

2 Answers

Jean Bosio
Jean Bosio
1,048 Points

I found this, by googling. Is it correct?

http://wordpress.stackexchange.com/questions/198704/should-i-use-loop-in-the-single-php-file

Answer 1: Stricktly speaking, you don't need the loop in single post pages, you only need the call to the_post(). The reason for the_post() is that the_post() sets the $post global value to the single post post object.

AND

Answer 2: If you want to maintain 100% compatibility with all plugins, you need to use the loop on single.php. The reason is that there are loop_start and loop_end actions which get triggered on the first and last call to have_posts().

Liam Maclachlan
Liam Maclachlan
22,805 Points

That would seem to make sense. I know I don't use the loop on single pages, but I think I may start :) It would seem the second one is to do with hooks that the plugins may use.

Jean Bosio
Jean Bosio
1,048 Points

Thanks, Liam :-)