WP – Basic Loop Explanation

see also:

  • https://developer.wordpress.org/reference/functions/the_post_thumbnail/
  • https://codex.wordpress.org/The_Loop_in_Action
  • https://codex.wordpress.org/The_Loop
  • https://developer.wordpress.org/reference/classes/wp_query/

Loop - Conditional / Object Orientation


There are other Conditional Tags that can be used to control the output depending on whether or not a particular condition is true with respect to the requested page.

Object orientation

Loops are a combination of object oriented and global behaviour. This is confusing at first.

The two important global variables for loops are:

  • $wp_query which is an object of class WP_Query, holding a WP database query result amongst which $wp_query->posts, an array of individual WP_Posts.
  • $post which is the current object of class WP_Post

have_posts() and the_post() are global functions calling the corresponding $wp_query->have_posts() and $wp_query->the_post() methods of the $wp_query global variable.

the_post() looks like a template tag, but it isn’t. It does not produce output, but instead changes the state of the $wp_query and $post global variables: the_post() tells WordPress to move to the next post. It changes $wp_query->current_post, and initialises the $post global variable to the next post contained in $wp_query->posts array.

Remember: All the template tags rely on the $post global variable by default and the $post global variable is set/modified by the_post(), which gets its data from the $wp_query global variable. $post is also set/modified by WP_Query::the_post() as used in secondary loops.


https://www.wpbeginner.com/glossary/loop/

The loop, or WordPress loop or simply loop, is PHP code that displays WordPress posts. The loop is used in WordPress themes to display a list of posts in a web page.

Inside the loop there are some functions that are run by default to display posts. Theme developers can format the output by using template tags to customize how each post inside the loop is displayed. There are several Template tags that work only inside the WordPress loop and can be used to format, arrange, and publish post data. The WordPress loop is arguably one of the most important aspects of the WordPress code and at the core of most queries in one way or another.

Understanding the WordPress Loop – Infographic

We have created an infographic to break down the WordPress Loop for beginners.

Infographic - Understanding the WordPress Loop

An example usage of a simple WordPress loop:


<?php
 
// checks if there are any posts that match the query
if (have_posts()) :
 
  // If there are posts matching the query then start the loop
  while ( have_posts() ) : the_post();
 
    // the code between the while loop will be repeated for each post
    ?>
 
    <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
 
    <p class="date-author">Posted: <?php the_date(); ?> by <?php the_author(); ?></p>
 
    <?php the_content(); ?>
 
    <p class="postmetadata">Filed in: <?php the_category(); ?> | Tagged: <?php the_tags(); ?> | <a href="<?php comments_link(); ?>" title="Leave a comment">Comments</a></p>
 
    <?php
 
    // Stop the loop when all posts are displayed
 endwhile;
 
// If no posts were found
else :
?>
<p>Sorry no posts matched your criteria.</p>
<?php
endif;
?>

 

Scroll to Top