Chapter 5

The Loop

WHAT’S IN THIS CHAPTER?

  • Understanding the flow of the Loop and where it can be used
  • Determining content display using the Loop
  • Customizing the Loop with different granularities of data access
  • Using template tags
  • Understanding global variables and their relationship to Loop processing
  • Working outside of the Loop

The Loop refers to how WordPress determines what content (posts, pages, or custom content) to display on a page you are visiting. The Loop can display a single piece of content or a group of posts and pages that are selected and then displayed by looping through the content; thus, it’s called the Loop.

This is how WordPress displays blog posts by default. The Loop selects posts from the MySQL database based on a set of parameters, and those parameters are typically determined by the URL used to access your WordPress website. For example, the homepage might show all blog posts in reverse chronological order by default. A category page, accessed via a URL such as http://example.com/category/zombies, shows only blog posts assigned to that category, in this case pages put into the zombies category. An archive page shows only blog posts that are dated with that particular month and year. WordPress maps nearly every parameter about your posts into a selection variable, providing the basis for an equally wide number of different ways to alter the Loop’s content selection algorithm. It is very easy to customize what content is displayed, and where, on your website ...

Get Professional WordPress: Design and Development, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.