Did you ever want to edit the look of your blog posts you publish regularly through your WordPress-based blog? If you did, this is the right article for you. It will introduce you into the magic of the WordPress Loop - a few lines of code rolling the significant part of the blogosphere around.
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:
<?php
// The Loop
if (have_posts()) : while (have_posts()) : the_post();
…
endwhile; else:
…
endif;
?>
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.
Default Loop
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:
1.
2.
3. 5. ID, 'PostThumb',true); ?> 6. 7. Posted on <?php the_time(‘F jS, 8. Y’); ?> by 9. 10. 11. '); ?> 12. Posted in 13. <?php comments_popup_link(‘No Comments;’, 14. ‘1 Comment’, ‘% Comments’); ?> 15.
16.
17.
18.
19.
20. Nothing Found
21.
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