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?
1 Answer
Reset to default 0It 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
版权声明:本文标题:loop - Wordpress theme files Organization 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326961a2453918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论