Well the Loop, it is simply a recursive call that is responsible for going through the number of posts that we have in our database and shows them according to the criteria that we tell it, its functionality is complex but its use is easy on the nose.
The code.
I'm going to try to compress the code as much as possible to make the post as small as possible since then I get giant posts that no one wants to read.
In this code we see the famous Loop in bold, it really is 4 lines, but they have a lot of strength. Let's gut the code a bit more.
First we can see two functions that give the Loop a lifetime.
These lines (for those who know something about the language of PHP programming, they will see that this is bullshit, that they go to another blog to read interesting things) they are in charge of checking that there are posts and if there are posts we go through them. The function have_posts ()
, it is responsible for returning the posts that we are filtering when going through the index, a dark and sinister process that surely more than one would not be prepared for it (me among them).
have_posts ()
, is responsible for differentiating the posts that are in draft and those who are inpublish. It is also responsible for differentiating if the posts are included between the dates that we are looking for, categories or other criteria that we may wish to search.
With the while we make it go through them while it finds posts, the process of the objects that it creates to carry out this tour really seem very complicated to explain the operation of the Loop and it is useless to put it into operation, the magic of OOP.
With the_post ()
, we load in memory all data of the current post. And after him all calls that we need to show the post data will refer to the current post, this allows us to assemble curious constructions for our templates.
A simple idea without any originality would be to mount the one we have in the Kubrick : D, but it helps us to show how it works.
by
In this way we assemble our first Loop with a more crappy appearance than you have ever seen, but less gives a stone ...
We are going to finish our Loop, for this we will have to close the previous tags.
With this tag we will close the while that ran through the posts that we were going to show, now we only need a beautiful, distinctive and attention-grabbing system to be able to move through the different pages with their respective Loops.
Now with our super nice Loop armed we have to think that it may be the case that there are no posts to show, then we will have to contemplate this possibility with the following code.
It has not been found
Now if we already have our Loop well armed, we only need to close it (mainly because if we do not do it, we will not see more than a php error), and for this:
If you have followed the steps you will now have a file similar to this (perhaps in Spanish):
by
Not found
Well, you're done with this, you can start assembling your own Loop and formatting all the knowledge that I tried to convey to you. You can also take a look at the WordPress codex And if you understand something (not because of the language but because it is a bit confusing) you will be able to find very curious things.
No Comment