Last Updated:

What is the WordPress Loop – The Loop


To begin with, saying "loop" is wrong. It's the same as saying "loop cycle", i.e. loop = loop = repetitive action. The correct thing to say is simply "WordPress Cycle". Further in the text, "WordPress Loop" is the same as "loop", "the loop" or "WordPress loop".

The WordPress loop (The Loop) is a search of posts (posts) and the output of any information about each post. The loop receives an array of records (objects), iterates through this array and displays information about each post during the search. At the same time, the loop uses functions specially created for it: the_title(),, the_permalink(),, the_date(), etc.

When you say "Function is used inside a WordPress Loop", it means that the function can be used inside a loop, and there is no need to pass parameters to it, because the function is designed to be used within a loop.

For example, the_title() is called without parameters, but the function will display the title of the weaving record in a loop. The output data is taken from the global variable .$post

The loop in WordPress looks like this:

<?php if ( have_posts() ) : while ( have_posts()  ) : the_post(); ?> <!-- WordPress Loop --> <p>Infers the data of the record. Functions for the loop, for example, the_title() </p> <h2> <?php the_title() ?></h2> <?php endwhile; else : ?> <p>Noscenes. </p> <?php endif; ?>

Or you can write it like this:

<?php if ( have_posts() ) { while ( have_posts() ) { the_post(); ?> <!-- WordPress Loop --> <!-- Output posts: the_title(), etc. --> <?php } } else { ?>  <p> There are no recordings. </p>  <?php } ?>

You can also write it like this (the "No Records" part in this case can be deleted):

<?php while ( have_posts() ){ the_post(); ?> <!-- WordPress Loop --> <!-- Output posts: the_title(), etc. --> <?php } ?> <?php if ( ! have_posts() ) { ?> <p> There are no recordings. </p> <?php } ?>

All information about the record (post) is stored in a global variable. Suppose that the loop processes an array containing the data of 10 posts, then the loop will have 10 repetitions and the variable will change 10 times, and with each repetition with the variable, the information about the post will be read and displayed on the screen, through the template tags: , ... Thus, any PHP/HTML code placed in a WordPress loop will be repeated while the cycle is running: 10 iterations of the loop - 10 repetitions.$post$post$postthe_title()the_content()

A typical example of a Loop is the output of posts on the category page, tags, on the main page of WordPress.

Complete sample series

Here's an example of a WordPress loop with comments embedded in the code (inside <!-- -->)

<!-- Check for entries in loop --> <?php if ( have_posts() ) : ?> <!-- Loop Start --> <?php while ( have_posts() ) : the_post(); ?> <!-- WordPress Loop -- > <!-- The variable $post has already been determined here, --> <!-- on the basis of which further code will be built. --> <!-- $post will change for each post while( have_posts() ). --> <!-- $post need the template tags to work: in_category('3'), the_permalink(), etc. --> <!-- Check if this post is in category 3. -->  <!-- If so, then set the CSS class div-u class="post-cat-three". --> <!-- If not, the class will be post class="post". --> <?php if ( in_category('3') ) { ?> <div class="post-cat-three"> <?php } else { ?> <div class=" post"> <?php } ?> <!-- Display the title of the post as a link to the post itself. --> <h2><a href="<?php the_permalink() ?>" title="Link to: <?php the_title_ attribute(); ?>"><?php the_title(); ?> </a></h2> <!-- We display the date of the post and a link to other posts by the author. --> <small><?php the_time('F jS, Y') ?> Author: <?php the_author_posts_link() ?></small>  <!-- Display the text of the post in the div tag. --> <div class="entry"> <?php the_content(); ?> </div> <!-- Output post categories, separated by commas. --> <p class="postmetadata">Based in <?php the_category(', ') ; ?> </p> </div> <!-- close the main div tag --> <!-- From here the loop starts to repeat if there are still posts --> <!-- Stop the loop (endwhile), --> <?php endwhile; ?> <!-- CompleteAnd the cycle. --> <!-- If there are no entries in the loop , the loop did not work (else) --> <?php else: ?> <p>No posts in the loop. </p> <?php endif; ?>

Other cycle options

The examples of the loop above, where it is used, get the search data from the global variable $wp_query to which WP automatically adds the data according to which page we are currently on, for example, on the main page or the category page, the data will be different.have_posts()

But, we can get data and then process them in a cycle on our own.

Loop using get_posts():

<?php global $post; $myposts = get_posts( 'numberposts=5&offset=1&category=1' ); foreach( $myposts as $post ){ setup_postdata( $post ); ?>  <!-- here is the formation of the output of posts where template tags related to the loop work, for example, the_title() --> <?php } wp_reset_postdata(); ?>

Loop using wp_query():

 set the necessary criteria for selecting data from the database $query = new WP_Query( [ 'posts_per_page' = > 5, 'orderby' = > 'comment_count' ] ); global $post loop; if ( $query- >have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); <?php the_title() ?> } } else {  Posts Not Found } wp_ reset_postdata();  Reset the $post. Return the original data

Why do we need to understand inside the Cycle or not?

It is necessary to distinguish where the WordPress Loop is used, and where the code is outside the loop, because in WordPress there are certain template tags that do not work outside the loop, for example: the_title(), the_content(), the_excerpt(), etc. In order for each such template tag to work, a variable must be defined that is unknown outside the WordPress loop (or rather not correct, if the loop was started, then known - it contains the data of the last one processed in the post cycle, but that's another topic).$post$post