Codex The Loop – Developing WordPress Themes Part Seven

Codex The Loop – Developing WordPress Themes Part Seven

The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.

When WordPress documentation says “This tag must be within The Loop”, such as for specific Template Tags or plugins, the tag will be repeated for each post. For example, The Loop displays the following information by default for each post:

  • Title (the_title())
  • Time (the_time())
  • Categories (the_category()).

You can use The Loop for a number of different things, for example to:

  • display titles and excerpts of posts on the home page of your blog;
  • View content and comments on a single post;
  • Display content on a single page using template tags; And
  • View data from custom post types and custom fields .

You can customize the loop via template files to display and manipulate different content.

Episode The Loop in detail #

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        // Display post content
    endwhile; 
endif; 
?>

This loop indicates that when there are posts, hover over and view them. Divided into more details:

  • The have_posts() function checks for any posts.
  • If there are posts, the while loop continues as long as the condition in the parentheses is logically true. As long as have_posts() remains true, the loop will continue.

Use The Loop #

The loop should be placed in index.php and in any other template file used to display the post information, for example:

<?php 
get_header();
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        // Display post content
    endwhile; 
endif; 
?>

In the example above, the end of the loop is shown with endwhile  and endif. The loop should always start with the same if and while statements, as above, and it should end with the same ending statements.

Any template tags you might want to apply to all posts should exist between the start and end statements.

<?php
get_header();
 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post();
        the_content();
    endwhile;
else :
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
endif;
 
get_sidebar();
get_footer();
?>

What The Loop #Can Show

A loop can display a number of different elements for each post. For example, some common template tags used in many themes are

next_post_link()  
previous_post_link()  
the_category() 
the_author()  
the_content()  
the_excerpt() 
the_ID()  
the_meta() 
the_shortlink()  
the_tags()  
the_title()  
the_time()  

You can also use conditional tags, like:

is_home() 
is_admin() 
is_single() 
is_page() 
is_page_template() 
is_category() 
is_tag()
is_author()
is_search()
is_404() 
has_excerpt()

#The Loop Examples

<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        the_title( '<h2>', '</h2>' ); 
        the_post_thumbnail(); 
        the_excerpt();
    endwhile; 
else: 
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); 
endif; 
?>
<?php 
if ( have_posts() ) : 
    while ( have_posts() ) : the_post(); 
        the_title( '<h1>', '</h1>' ); 
        the_content();
    endwhile; 
else: 
    _e( 'Sorry, no pages matched your criteria.', 'textdomain' ); 
endif; 
?>
<?php
// Start the Loop.
if ( have_posts() ) : 
    while ( have_posts() ) : the_post();
        /* * See if the current post is in category 3.
          * If it is, the div is given the CSS class "post-category-three".
          * Otherwise, the div is given the CSS class "post".
        */
        if ( in_category( 3 ) ) : ?>
        <div class="post-category-three">
        <?php else : ?>
        <div class="post">
        <?php endif; 
 
            // Display the post's title.
            the_title( '<h2>', ';</h2>' ); 
 
            // Display a link to other posts by this posts author.
            printf( __( 'Posted by %s', 'textdomain' ), get_the_author_posts_link() );
 
            // Display the post's content in a div.
            ?>
            <div class="entry">
                <?php the_content() ?>
             </div>
     
            <?php
            // Display a comma separated list of the post's categories.
            _e( 'Posted in ', 'textdomain' ); the_category( ', ' ); 
 
        // closes the first div box with the class of "post" or "post-cat-three"
       ?>
       </div>
 
    <?php 
    // Stop the Loop, but allow for a "if not posts" situation
    endwhile; 
 
else :
    /*
      * The very first "if" tested to see if there were any posts to
      * display. This "else" part tells what do if there weren't any.
     */
     _e( 'Sorry, no posts matched your criteria.', 'textdomain' );
  
// Completely stop the Loop.
 endif;
?>
<?php
// The main query.
if ( have_posts() ) : 
    while ( have_posts() ) : the_post();
        the_title();
        the_content();
    endwhile;
else :
    // When no posts are found, output this text.
    _e( 'Sorry, no posts matched your criteria.' );
endif;
wp_reset_postdata();                                                        
 
/*
 * The secondary query. Note that you can use any category name here. In our example,
 * we use "example-category".
 */
$secondary_query = new WP_Query( 'category_name=example-category' );        
 
// The second loop. 
if ( $secondary_query->have_posts() )
    echo '<ul>';
    while ( $secondary_query->have_posts() ) : $secondary_query->the_post();
        the_title( '<li>', '</li>' );
     endwhile;
     echo '</ul>';
endif;
wp_reset_postdata();
?>
<?php 
// Example argument that defines three posts per page. 
$args = array( 'posts_per_page' => 3 ); 
 
// Variable to call WP_Query. 
$the_query = new WP_Query( $args ); 
 
if ( $the_query->have_posts() ) : 
    // Start the Loop 
    while ( $the_query->have_posts() ) : $the_query->the_post(); 
        the_title(); 
        the_excerpt(); 
    // End the Loop 
    endwhile; 
else: 
// If no posts match this query, output this text. 
    _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); 
endif; 
 
wp_reset_postdata(); 
?>