The WordPress Loop

The WordPress Loop

Bfacf748069915ec6f7ed4ac8d5457d7?s=128

Giustino Borzacchiello

January 12, 2016
Tweet

Transcript

  1. 4.

    The standard Loop “The Loop is PHP code used by

    WordPress to display posts.” https://codex.wordpress.org/The_Loop
  2. 5.

    The standard Loop “The Loop is PHP code used by

    WordPress to display posts.” https://codex.wordpress.org/The_Loop
  3. 6.

    The standard Loop “The Loop is PHP code used by

    WordPress to display posts.” https://codex.wordpress.org/The_Loop
  4. 13.

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

    // Single post endwhile; else: // No post found endif;
  5. 14.

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

    // Single post endwhile; else: // No post found endif;
  6. 15.

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

    // Single post endwhile; else: // No post found endif;
  7. 16.

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

    // Single post endwhile; else: // No post found endif;
  8. 17.

    ... while ( have_posts() ): the_post(); <h2><?php the_title(); ?></h2> <?php

    the_excerpt(); ?> endwhile; else: // No post found endif;
  9. 18.

    ... while ( have_posts() ): the_post(); <h2><?php the_title(); ?></h2> <?php

    the_excerpt(); ?> endwhile; else: // No post found endif; Template tags
  10. 19.

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

    // Single post endwhile; else: // No post found endif;
  11. 20.

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

    // Single post endwhile; else: // No post found endif;
  12. 22.

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

    the_title(); ?></h2> <?php the_content(); ?> endwhile; endif;
  13. 25.

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

    have_posts() ): the_post(); if( 0 === $count ) { // first post } else { // other posts } $count += 1; endwhile; endif;
  14. 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();
  15. 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();
  16. 31.

    The standard Loop Try to find and understand the loop

    in this theme https://github.com/WordPress/twentysixteen
  17. 32.

    The standard Loop How does WordPress find what posts to

    show? https://developer.wordpress.org/themes/basics/template-hierarchy/
  18. 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;