The
WordPress
Loop
Giustino Borzacchiello / WordPress Meetup Milano - January 2016
The standard Loop
What’s The Loop?
The standard Loop
“The Loop is PHP code used by WordPress to display posts.”
The standard Loop
“The Loop is PHP code used by WordPress to display posts.”
The standard Loop
“The Loop is PHP code used by WordPress to display posts.”
The standard Loop
Post list
The standard Loop
Single post/page
The standard Loop
“Mullet” loop
The standard Loop
Random post widget
The standard Loop
Related posts
The standard Loop
The Loop breakdown
The standard Loop
<?php
if ( have_posts() ):
while ( have_posts() ):
the_post();
// Single post
endwhile;
else:
// No post found
endif;
<?php
if ( have_posts() ):
while ( have_posts() ):
the_post();
// Single post
endwhile;
else:
// No post found
endif;
<?php
if ( have_posts() ):
while ( have_posts() ):
the_post();
// Single post
endwhile;
else:
// No post found
endif;
<?php
if ( have_posts() ):
while ( have_posts() ):
the_post();
// Single post
endwhile;
else:
// No post found
endif;
...
while ( have_posts() ):
the_post();
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
endwhile;
else:
// No post found
endif;
...
while ( have_posts() ):
the_post();
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
endwhile;
else:
// No post found
endif;
Template tags
<?php
if ( have_posts() ):
while ( have_posts() ):
the_post();
// Single post
endwhile;
else:
// No post found
endif;
<?php
if ( have_posts() ):
while ( have_posts() ):
the_post();
// Single post
endwhile;
else:
// No post found
endif;
Single Loop
The standard Loop
if ( have_posts() ):
while ( have_posts() ):
the_post();
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
endwhile;
endif;
“Mullet” Loop
The standard Loop
1
2
3
4
5
The standard Loop
<?php
if ( have_posts() ):
$count = 0;
while ( have_posts() ):
the_post();
if( 0 === $count ) {
// first post
} else {
// other posts
}
$count += 1;
endwhile;
endif;
Random posts Loop
The standard Loop
<?php
$rand_query = new WP_Query(['orderby' => 'rand']);
if ( $rand_query->have_posts() ):
while ( $rand_query->have_posts() ):
$rand_query->the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
<?php
$rand_query = new WP_Query(['orderby' => 'rand']);
if ( $rand_query->have_posts() ):
while ( $rand_query->have_posts() ):
$rand_query->the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
Useful references
The standard Loop
The loop and template tags
https://developer.wordpress.org/themes/basics/the-loop/
The standard Loop
Try to find and understand the loop in this theme
The standard Loop
How does WordPress find what posts to show?
https://developer.wordpress.org/themes/basics/template-hierarchy/
The standard Loop
The standard Loop
The standard Loop
Extra: related posts loop
The standard Loop
if ( have_posts() ):
while ( have_posts() ):
the_post();
// EXERCISE! Insert here template tags
$rel_query = new WP_Query([
'category__in' => wp_get_post_categories($post->ID),
'post__not_in' => [$post->ID],
'posts_per_page' => 3,
]);
// EXERCISE! Insert here $rel_query loop!
endwhile;
endif;
Images
https://www.flickr.com/photos/nkphillips/2984667077/
https://www.flickr.com/photos/jeremybrooks/2214481087/
https://www.flickr.com/photos/benjaminbeard/5642453196/
https://www.flickr.com/photos/jpestana/16315778523/
https://www.flickr.com/photos/xroper7/19998325086/
https://www.flickr.com/photos/katsrcool/16912193533/
https://www.flickr.com/photos/15609463@N03/9848807935/
https://www.flickr.com/photos/adamfowler/4841559945/
https://www.flickr.com/photos/donald_gunn/15340138404/
https://www.flickr.com/photos/trevira1/8218299403/