Introduction to WordPress Loop
The WordPress Loop is an essential core part of every WordPress theme. These several lines of code are responsible for millions and millions of published blog posts every single day. According to the definition available on an official WordPress documentation website (also known as the WordPress codex), with the loop “WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within the Loop tags.” In other words, the Loop serves to pull out the posts from the WordPress database.
Basic WordPress Loop
In its simplest form, the Loop looks like this:
// The Loop
if (have_posts()) : while (have_posts()) : the_post();
In this case, the Loop will check first if there are any posts. If there are, the Loop begins and the_post() function prepares the stage for inner-loop functions. Once the Loop has iterated a specific number of times (as set within the WordPress), the have_posts() function turns to false and the loop ends.
Although the source code of the Loop mentioned above can publish the posts, it is used rarely, as blog owners usually want to publish some other information and features the Loop allows. Therefore, the complete “default” Loop looks like this:
4. <?php the_title(); >
7. Posted on <?php the_time(‘F jS,
8. Y’); ?> by
12. Posted in
13. <?php comments_popup_link(‘No Comments;’,
14. ‘1 Comment’, ‘% Comments’); ?>
20. Nothing Found
Explanations of the Loop Functions
If a developer wants to modify the Loop, it is very important to know which lines are editable, which lines aren’t, and finally, where additional features (lines of code) can be added. When considering the explanation of functions, a reader should refer to the numbering as outlined in the previous section (that numbering actually presents the lines of code), as the lines will not always be repeated here.
- First of all, lines 1) and 2) are already explained in the first paragraph. To be noted here, they are mandatory and as such, must not be removed. Line 3) creates a division (post area) were the post content will be stored. It also pulls out the information about post ID from the database.
- Furthermore, the code between the and the closing tags create the post title (with an anchor link, linking to a single post page).
- The line of code 5) pulls out the information from the custom field (if any) and displays it.
- Everything between the and pulls out the Meta information concerning specific post.
- The code pulls out the posts content from the database, and as such, there are no doubts about its importance.
- Furthermore, the lines of code after the content function and before the tag are responsible for pulling out the Meta information. In this case, tags, category and number of comments assigned to this post will be displayed.
- After the , there is a function, responsible for ending the inner loop functions.
- Two functions and are responsible for displaying the links to older entries (if any) and to newest entries (if on a page containing older entries).
- If there are no posts published , will confirm it and Nothing Found will be displayed.
- Finally, ends the complete Loop.
As this WordPress Loop examination has showed, some lines of the Loop code cannot be omitted, while on the other side, the potential of Loop customization is huge, as developers can create their own functions and implement them easily into this core of every WordPress blog.