Phil Kurth
/ Tips /

Use PHP's alternative syntax for clearer WordPress templates

Last updated • 26 October 2019

I’ve seen quite a few instances of people asking when and where to use PHP’s alternative syntax for control structures and if you don’t know what I’m referring to, check out the PHP documentation for an outline.

There’s no hard and fast rule for when to use the alternate syntax but personally, I much prefer the alternative syntax for template use. I never use it in blocks of PHP code but I always use it when I’m writing templates. I just find it a whole lot more readable than trying to find the closing brace wrapped in PHP tags in amongst a bunch of HTML markup.

Here is a look at what a basic WordPress template looks like when using the alternative syntax:

<?php

get_header();
?>
<div class="Main">

	<?php if ( have_posts() ): ?>
		<?php while ( have_posts() ): ?>
			<?php the_post(); ?>
            
			<div class="Post">

			</div>
            
		<?php endwhile; ?>
	<?php endif; ?>

</div>
<?php
get_footer();

The endif and endwhile tags are just so much clearer in amongst template code.

Need a website?
Let's talk.

From website design & SEO through to custom WordPress plugin development. I transform ideas into dynamic, engaging, and high-performing solutions.

Phil Kurth, web designer and developer in Geelong