Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The WordPress Loop

The WordPress Loop

Giustino Borzacchiello

January 12, 2016
Tweet

More Decks by Giustino Borzacchiello

Other Decks in Technology

Transcript

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

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

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

    WordPress to display posts.” https://codex.wordpress.org/The_Loop
  4. <?php if ( have_posts() ): while ( have_posts() ): the_post();

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

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

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

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

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

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

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

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

    the_title(); ?></h2> <?php the_content(); ?> endwhile; endif;
  13. <?php if ( have_posts() ): $count = 0; while (

    have_posts() ): the_post(); if( 0 === $count ) { // first post } else { // other posts } $count += 1; endwhile; endif;
  14. <?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. <?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. The standard Loop Try to find and understand the loop

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

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