WordPress Theme: Part 3

If you’ve gotten to this point, well done! That was, unfortunately, the easy part. You should have header.php and footer.php uploaded to your theme folder blocks, and you should have index.php open in front of you with the following code:

<?php get_header(); ?>

<?php get_footer(); ?>

All the hard stuff we’re going to do will be between those two functions. If you think you’ll get confused, simply make another div between them, and do all your work inside the div.

The Loop

The Loop is quite possibly the scariest part of WordPress, but if you know what you’re doing and leave large spaces in your code to ensure you don’t get confused – well, you won’t get confused.

The Loop is used by WordPress to display your posts. WordPress processes each of the posts to be displayed on the page, by way of The Loop. The posts are formatted according to how you specify them inside The Loop itself. At the WordPress Codex, you can read about how The Loop works or The Loop in general.

The Loop starts out with this code:

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

Paste that in your index.php document. Remember, we’re working in between the get_header and get_footer functions.

The part of The Loop I’ve just shown you is the part that checks if there are posts with the have_posts() function. In summary, the function have_posts() checks if there is an item in the collection of posts. If the result returns true, The Loop keeps going as long as there are posts to display. If there is a false return it means that there are no more posts.

We end The Loop with this:

<?php endwhile; ?>
<?php endif; ?>

The Loop unfortunately does get more complicated but we’ll get onto that in the next section. Save your document for the time being.