2952 posts
  • Attended a Community Meetup
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Bought between 100 and 499 items
  • Contributed a Blog Post
  • Elite Author
  • Exclusive Author
+5 more
DDStudios says

I am learning wordpress right now and I’ve been suggested to write the code a certain way.

I trust the guy but I wanted to know what you guys think, what is better? What does user prefer?

So here’s an example :

This code :

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

Compared to this one :

<?php if (have_posts()) {
    while (have_posts()) { 

    the_post(); ?> 

Do you guys always close your php tag even if there is more PHP coming?

Thanks!

444 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • United Kingdom
TahaH-Studio says

For template files I use short tags eg. if (have_posts()) : while (have_posts()) : the_post();. Its just because its more organised in some way. But in functions.php and other fully php files I use the normal php codeing

1028 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Author had a File in an Envato Bundle
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 500 and 999 users
+1 more
ThemeBlvd says

To keep things clean with a bunch of PHP mixed in with my HTML in complicated Wordpress themes, I do all my basic if/elses and loops by line.

So in your example, I would do:

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

I don’t like seeing the PHP tags more than once in a line unless it’s mixed into some html like:

<a href="<?php the_permalink(); ?>" title="<?php the_title; ?>" />
69 posts
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 100 and 1 000 dollars
  • Sri Lanka
vinoj says

I don’t see any good reason for creating more and more php tags like the first one ;)

Bro as i know both codes are same, and they do exactly the same thing. However, I prefer to use second one as I can organize my codes well with that way, and it is easy to find any error with that code structure too. Both codes are same, only the structures are different.

374 posts
  • Bought between 10 and 49 items
  • Contributed a Blog Post
  • Contributed a Tutorial to a Tuts+ Site
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
_rohan says

Dont close PHP tags when there’s no need to, but make sure you indent the code properly.

by
by
by
by
by
by