1 of 37

The

WordPress

Loop

Giustino Borzacchiello / WordPress Meetup Milano - January 2016

2 of 37

The standard Loop

3 of 37

What’s The Loop?

The standard Loop

4 of 37

“The Loop is PHP code used by WordPress to display posts.”

The standard Loop

5 of 37

“The Loop is PHP code used by WordPress to display posts.”

The standard Loop

6 of 37

“The Loop is PHP code used by WordPress to display posts.”

The standard Loop

7 of 37

Post list

The standard Loop

8 of 37

Single post/page

The standard Loop

9 of 37

“Mullet” loop

The standard Loop

10 of 37

Random post widget

The standard Loop

11 of 37

Related posts

The standard Loop

12 of 37

The Loop breakdown

The standard Loop

13 of 37

<?php

if ( have_posts() ):

while ( have_posts() ):

the_post();

// Single post

endwhile;

else:

// No post found

endif;

14 of 37

<?php

if ( have_posts() ):

while ( have_posts() ):

the_post();

// Single post

endwhile;

else:

// No post found

endif;

15 of 37

<?php

if ( have_posts() ):

while ( have_posts() ):

the_post();

// Single post

endwhile;

else:

// No post found

endif;

16 of 37

<?php

if ( have_posts() ):

while ( have_posts() ):

the_post();

// Single post

endwhile;

else:

// No post found

endif;

17 of 37

...

while ( have_posts() ):

the_post();

<h2><?php the_title(); ?></h2>

<?php the_excerpt(); ?>

endwhile;

else:

// No post found

endif;

18 of 37

...

while ( have_posts() ):

the_post();

<h2><?php the_title(); ?></h2>

<?php the_excerpt(); ?>

endwhile;

else:

// No post found

endif;

Template tags

19 of 37

<?php

if ( have_posts() ):

while ( have_posts() ):

the_post();

// Single post

endwhile;

else:

// No post found

endif;

20 of 37

<?php

if ( have_posts() ):

while ( have_posts() ):

the_post();

// Single post

endwhile;

else:

// No post found

endif;

21 of 37

Single Loop

The standard Loop

22 of 37

if ( have_posts() ):

while ( have_posts() ):

the_post();

<h2><?php the_title(); ?></h2>

<?php the_content(); ?>

endwhile;

endif;

23 of 37

“Mullet” Loop

The standard Loop

24 of 37

1

2

3

4

5

The standard Loop

25 of 37

<?php

if ( have_posts() ):

$count = 0;

while ( have_posts() ):

the_post();

if( 0 === $count ) {

// first post

} else {

// other posts

}

$count += 1;

endwhile;

endif;

26 of 37

Random posts Loop

The standard Loop

27 of 37

<?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();

28 of 37

<?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();

29 of 37

Useful references

The standard Loop

30 of 37

The standard Loop

31 of 37

Try to find and understand the loop in this theme

https://github.com/WordPress/twentysixteen

The standard Loop

32 of 37

The standard Loop

33 of 37

The standard Loop

34 of 37

thank you

@jubstuff

borzacchiello.it

The standard Loop

35 of 37

Extra: related posts loop

The standard Loop

36 of 37

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;

37 of 37

Images