admin管理员组

文章数量:1332345

I am new at WordPress themes development, and I am willing to build a complete theme, while I still at the index.php, I found that the file itself becomes more complicated, a lot of HTML tags comes interleaving with PHP code and loops, I think it would be difficult to remain at the structure, so is there any defined structure to make the code more organized and editable at the feature?

I am new at WordPress themes development, and I am willing to build a complete theme, while I still at the index.php, I found that the file itself becomes more complicated, a lot of HTML tags comes interleaving with PHP code and loops, I think it would be difficult to remain at the structure, so is there any defined structure to make the code more organized and editable at the feature?

Share Improve this question asked Jun 22, 2020 at 16:59 AbdelrahmanAbdelrahman 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It seems like the Wordpress community really loves this mix of PHP and HTML together, and as you say it often leads to very hard to understand and maintain code.

It is a common programming pattern to separate out 'logic' and 'presentation'. Logic here is PHP code which makes decisions, presentation is HTML.

I strongly recommend trying to separate the two whenever you can, and put as much PHP in one place, and as much HTML in another place.

For example you might have:

<b>Your name: <?php if user_logged_in() { $name=get_user_name(); echo $name; }  else { echo "please log in!"; } ?></b>

To separate that out you could write:

<?php 
      if (user_logged_in()) {
            $name = get_user_name();
       } else { 
            $name = "please log in!";
       }
?>

<b>Your name: <?php echo $name ; ?></b>

This approach allows you to begin to move all your PHP and 'business logic' away from the HTML and the 'presentation' so that you can change the logic of the way things work separately from the way it's presented.

I hope this helps! There are even more advanced solutions like PHP templating engines which do the separation even better, but maybe this is overkill.

本文标签: loopWordpress theme files Organization