Use PHP’s alternative syntax for clearer WordPress templates

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:

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

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.
Subscribe to get the latest insights & updates in the world of web and how it impacts your business and website.
© 2024 Phil Kurth  |  All rights reserved.