We often see this insdie the WordPress templates:
while ( have_posts() ) : the_post();
...
endwhile;
Do you have any idea why not using braces or “curly brackets” {} for while
loop? What is the gain?
We often see this insdie the WordPress templates:
while ( have_posts() ) : the_post();
...
endwhile;
Do you have any idea why not using braces or “curly brackets” {} for while
loop? What is the gain?
According to the wordpress handbook on php coding standards, braces should be used for all blocks in the style shown below:
if ( condition ) {
action1();
action2();
} elseif ( condition2 && condition3 ) {
action3();
action4();
} else {
defaultaction();
}
The use of braces means single-statement inline control structures
are prohibited so it’s better to use the alternative syntax for
control structures (e.g.if
/endif
,while
/endwhile
) in this scenario,
especially in template files where PHP code is embedded within HTML:
<?php if ( have_posts() ) : ?>
<div class="hfeed">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID() ?>" class="<?php post_class() ?>">
<!-- ... -->
</article>
<?php endwhile; ?>
</div>
<?php endif; ?>