I'm a dirty PHP coder, please clean me!

Hi,

I am very new to PHP coding and am sure that this little piece of work could be a lot tidier! Any helpful suggestions would be gratefully received and acted upon.

<?php

	if (have_posts()) :
		while (have_posts()) : the_post(); ?>
			<div class="container">
				<article class="post">
				<!-- No title hyperlink on single page -->
				<?php
					if (is_single()) : ?><h2><?php the_title(); ?></h2>
					<?php else :
				?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
					 <?php endif;
				?>
				<p><?php the_content(); ?></p>
				</article>
			</div>
		<?php endwhile;

		else : 
			echo '<p>Sorry, no content found.</p>';

	endif;


?>

Kind thanks in advance,
Gary.

This is a standard wordpress loop what else do you want to do with it?

Hi,

It is not so much that I want to do anything more with it, I wondered whether it could be tidier, with fewer <?php ... ?>s ?

As I say, I am very new to PHP scripting, but I would like to get into good practices as early as possible.

Gary.

If you mix php with html you will always end up with a lot of starting and closing php tags because that’s the way to do it. You cannot insert html tags in php script so they need to be separated. Another way to output html through php is:

echo "<p>Content</p>';

but if you programm in wordpress environment it’s very common to do it like you did above.

1 Like

@przemoo83

Okay, that’s great; thank you for the feedback. :slight_smile: