- slide 1 of 5
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.
- slide 2 of 5
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.
- slide 3 of 5
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. <?php if (have_posts()) : ?>
2. <?php while (have_posts()) : the_post(); ?>
3. <div id="post-<?php the_ID(); ?>">
4. <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ></a></h2>
5. <?php echo get_post_meta($post->ID, 'PostThumb',true); ?>
7. <span>Posted on</span> <?php the_time('F jS,
8. Y'); ?> <span>by</span> <?php the_author(); ?>
10. <?php the_content('Read Full Article'); ?>
11. <p><?php the_tags('Tags: ', ', ', '<br />'); ?>
12. Posted in <?php the_category(', '); ?>
13. <?php comments_popup_link('No Comments;',
14. '1 Comment', '% Comments'); ?></p>
16. <?php endwhile; ?>
17. <?php next_posts_link('Older Entries'); ?>
18. <?php previous_posts_link('Newer Entries'); ?>
19. <?php else : ?>
20. <h2>Nothing Found</h2>
21. <?php endif; ?>
- slide 4 of 5
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 <h2> and the closing </h2> 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 <p> and </p> pulls out the Meta information concerning specific post.
- The <?php the_content('Read Full Article'); ?> 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 </div> 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 </div>, there is a <?php endwhile; ?> function, responsible for ending the inner loop functions.
- Two functions <?php next_posts_link('Older Entries'); ?> and <?php previous_posts_link('Newer Entries'); ?> 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 ,<?php else : ?> will confirm it and <h2>Nothing Found</h2> will be displayed.
- Finally, <?php endif; ?> ends the complete Loop.
- slide 5 of 5
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.