The WordPress Loop

The WordPress Loop

Bfacf748069915ec6f7ed4ac8d5457d7?s=128

Giustino Borzacchiello

January 12, 2016
Tweet

Transcript

  1. The WordPress Loop Giustino Borzacchiello / WordPress Meetup Milano -

    January 2016
  2. The standard Loop

  3. The standard Loop What’s The Loop?

  4. The standard Loop “The Loop is PHP code used by

    WordPress to display posts.” https://codex.wordpress.org/The_Loop
  5. The standard Loop “The Loop is PHP code used by

    WordPress to display posts.” https://codex.wordpress.org/The_Loop
  6. The standard Loop “The Loop is PHP code used by

    WordPress to display posts.” https://codex.wordpress.org/The_Loop
  7. The standard Loop Post list

  8. The standard Loop Single post/page

  9. The standard Loop “Mullet” loop

  10. The standard Loop Random post widget

  11. The standard Loop Related posts

  12. The standard Loop The Loop breakdown

  13. <?php if ( have_posts() ): while ( have_posts() ): the_post();

    // Single post endwhile; else: // No post found endif;
  14. <?php if ( have_posts() ): while ( have_posts() ): the_post();

    // Single post endwhile; else: // No post found endif;
  15. <?php if ( have_posts() ): while ( have_posts() ): the_post();

    // Single post endwhile; else: // No post found endif;
  16. <?php if ( have_posts() ): while ( have_posts() ): the_post();

    // Single post endwhile; else: // No post found endif;
  17. ... while ( have_posts() ): the_post(); <h2><?php the_title(); ?></h2> <?php

    the_excerpt(); ?> endwhile; else: // No post found endif;
  18. ... while ( have_posts() ): the_post(); <h2><?php the_title(); ?></h2> <?php

    the_excerpt(); ?> endwhile; else: // No post found endif; Template tags
  19. <?php if ( have_posts() ): while ( have_posts() ): the_post();

    // Single post endwhile; else: // No post found endif;
  20. <?php if ( have_posts() ): while ( have_posts() ): the_post();

    // Single post endwhile; else: // No post found endif;
  21. The standard Loop Single Loop

  22. if ( have_posts() ): while ( have_posts() ): the_post(); <h2><?php

    the_title(); ?></h2> <?php the_content(); ?> endwhile; endif;
  23. The standard Loop “Mullet” Loop

  24. The standard Loop 1 2 3 4 5

  25. <?php if ( have_posts() ): $count = 0; while (

    have_posts() ): the_post(); if( 0 === $count ) { // first post } else { // other posts } $count += 1; endwhile; endif;
  26. The standard Loop Random posts Loop

  27. <?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. <?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. The standard Loop Useful references

  30. The standard Loop The loop and template tags https://developer.wordpress.org/themes/basics/the-loop/ https://codex.wordpress.org/The_Loop

    https://codex.wordpress.org/The_Loop_in_Action
  31. The standard Loop Try to find and understand the loop

    in this theme https://github.com/WordPress/twentysixteen
  32. The standard Loop How does WordPress find what posts to

    show? https://developer.wordpress.org/themes/basics/template-hierarchy/
  33. The standard Loop WP_Query https://codex.wordpress.org/Class_Reference/WP_Query

  34. The standard Loop thank you @jubstuff borzacchiello.it

  35. The standard Loop Extra: related posts loop

  36. 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. 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/