Understanding how the Loop functions will help you understand how you can control it. Controlling the Loop to display exactly the content you want will be one of your most used tools in developing WordPress-powered web sites. Because the Loop is at the heart of every WordPress theme, being able to customize the display content opens up the doors to making WordPress look and act however you want.
To understand the Loop, it helps to break down the steps WordPress takes to generate a page's content:
> The URL is matched against existing files and directories in the WordPress installation. If the file is there, it is loaded by the web server. WordPress doesn't actually get involved in this decision; it's up to your web server and the .htaccess file created by WordPress to decide if the URL is something handled by the web server or to be turned into a WordPress content query. This was covered in Chapter 4.
> If the URL is passed to WordPress, it has to determine what content to load. For example, when visiting a specific tag page like http: //example.com/tag/bacon, WordPress will determine that you are viewing a tag and load the appropriate template, select the posts saved with that tag, and generate the output for the tag page.
> The translation of URL to content selection magic happens inside of the parse_query () method within the WP_Query object that WordPress created early on in its processing. WordPress parses the URL first into a set of query parameters that are described in the next section. All query strings from the URL are passed into WordPress to determine what content to display, even if they look like nicely formatted pathnames. If your site is using pretty permalinks, the values between slashes in those permalinks are merely parameters for query strings. For example, http://example.com/tag/bacon is the same as http: //example.com?tag=bacon, which conveys a query string of "tag with a value of bacon.''
> WordPress then converts the query specification parameters into a MySQL database query to retrieve the content. The workhorse here is the get_ posts () method within the WP_Query object that we describe later in this chapter. The get_ posts () method takes all of those query parameters and turns them into SQL statements, eventually invoking the SQL string on the MySQL database server and extracting the desired content. The content returned from the database is then saved in the WP_Query object to be used in the WordPress Loop and cached to speed up other references to the same posts made before another database query is executed.
> Once the content is retrieved, WordPress sets all of the is_ conditional tags such as is_home and is_ page. These are set as part of executing the default query based on the URL parsing, and we'll discuss cases where you may need to reset these tags.
> WordPress picks a template from your theme based on the type of query and the number of posts returned, for example, a single post or a category only query, and the output of the query is passed to this default invocation of the Loop.
The Loop can be customized for different web site purposes. For example, a news site might use the Loop to display the latest news headlines. A business directory could use the Loop to display local businesses alphabetically by name, or always put posts about sponsoring businesses at the top of every displayed page. A photo blog might use the Loop to display the most recent photos loaded into the web site. The possibilities are endless when customizing the Loop in WordPress because it gives you complete control over what content is selected and the order in which it is rendered for display.
Was this article helpful?
The Blogging From The Beginning course was specifically designed with these people in mind. It starts at the very beginning, by helping them choose the right platform and takes them all the way though creating content, getting traffic and making money from their blog. It is simple, straight forward information that will help them even if they've never heard of blogging before. It will teach them how to get started and quickly harness the power of blogging for themselves.