WordPress Loop

This post is to sum­ma­rize and present the code snip­pets of the most Word­Press fun­da­men­tal: the loop.

1. Set up index.php
< ?php get_header(); ?>
<div id="posts-container">
  // loop starts here
</div>
< ?php get_sidebar(); ?>
< ?php get_footer(); ?>
2. Sim­plest loop
if (have_posts()) :
  while (have_posts()) :
    the_post();
    the_content();
  endwhile;
endif;

Within the while loop:

  • Func­tion the_post() makes the cur­rent item in the posts col­lec­tion avail­able for use inside this iteration.
  • Tem­plate tag the_content() returns the post con­tent. more »