The WordPress Main Index Template
The WordPress Main Index template drags your blog posts out of the MySQL database and inserts them into your site. This template is to your blog what the dance floor is to a nightclub — where all the action happens.
The filename of the Main Index template is index.php. You can find it in the /wp-content/themes/twentyeleven/ folder.
The first template tag in the Main Index template calls in the Header template, meaning that it pulls the information from the Header template into the Main Index template, as follows:
<?php get_header(); ?>
Your theme can work without calling in the Header template, but it’ll be missing several essential pieces — the CSS and the blog name and tagline, for starters. Without the call to the Header template, your blog resembles the image shown.
The Main Index template in the Twenty Eleven theme calls in three other files in a similar fashion:
get_template_part( 'content', 'get_post_format' ); — this function calls in the template file named content.php as well as prepares the theme to accept special templates for a feature called Post Formats.
get_sidebar(); — this function calls in the template file named sidebar.php.
get_footer(); — this function calls in the template file named footer.php.
The Loop is a function that WordPress uses to display content on your site such as blog posts and page content. The Loop has a starting point and an ending point; anything placed in between is used to display each post, including any HTML, PHP, or CSS tags and codes.
Quite a few variations of the WordPress Loop are available. When you understand the common use, you can begin to push the envelope a bit further and use variations to suit your needs. This common use displays your posts in chronological order, starting with your most recent post and followed by less recent posts, ordered by date.
The basic and most common use of The Loop in WordPress is to pull blog posts from the database and display them on your website. In the default Twenty Eleven theme, you find the loop in a template called: loop.php. The beginning, or first line, of The Loop looks like this:
<?php while ( have_posts() ) : the_post(); ?>
At the bottom of the template, The Loop ends with one line of code:
<?php endwhile; ?>
Here’s a look at what the WordPress Codex calls The World’s Simplest Index.
<?php get_header(); if (have_posts()) : while (have_posts()) : the_post(); the_content(); endwhile; endif; get_sidebar(); get_footer(); ?>
First, the template starts by opening the php tag. Next, it includes the header, meaning that it retrieves anything contained in the header.php file and displays it. Now the good stuff starts happening. The Loop begins with the while (have_posts()) : bit. Anything between the while and the endwhile repeats for each post that displays. The number of posts displayed is determined in the settings section of the WordPress Dashboard.
Misplacement of the while or endwhile statements causes The Loop to break. If you’re having trouble with The Loop in an existing template, check your version against the original and see whether the while statements are misplaced.
In your travels as a WordPress user, you may run across plugins or scripts with instructions that say something like this: This must be placed within The Loop. Understanding The Loop arms you with the knowledge you need for tackling and understanding your WordPress themes.
The Loop is no different from any other template tag; it must begin with a function to start PHP, and it must end with a function to stop PHP. The Loop begins with PHP and then makes a request: While there are posts in my blog, display them on this page.
This PHP function tells WordPress to grab the blog post information from the database and return it to the blog page. The end of The Loop is like a traffic cop with a big red stop sign telling WordPress to stop the function completely.
You can set the number of posts displayed per page in the Reading Settings page on the WordPress Dashboard. The Loop abides by this rule and displays only the number of posts per page that you’ve set.
The big if variable
PHP functions in a pretty simple, logical manner. It functions by making decisions based on questions and answers. PHP deals with three basic variables:
The basic idea is this: IF this, THEN that, or ELSE this.
If your blog has posts (and most do, even when you first install it), WordPress proceeds with The Loop, starting with the piece of code that looks like this:
<?php while (have_posts()) : the_post(); ?>
This code tells WordPress to grab the posts from the MySQL database and display them on your blog page.
Then The Loop closes with this tag:
<?php endwhile; ?>
Near the beginning of The Loop template is a template tag that looks like this:
<?php if ( ! have_posts() ) : ?>
The exclamation point (!) preceding the function: have_posts() means does not. So a translation of that template tag into plain English reads: If [this blog] does not have posts.
If your blog meets that condition (that is, if it doesn’t have any posts), WordPress skips The Loop and displays a message that no posts exist:
<div id="post-0" class="post error404 not-found"> <h1 class="entry-title"><?php _e( 'Not Found', 'mytheme' ); ?></h1> <div class="entry-content"> <p><?php _e( 'Apologies, but no results were found for the requested Archive. Perhaps searching will help find a related post.', 'mytheme' ); ?></p> <?php get_search_form(); ?> </div><!-- .entry-content → </div><!-- #post-0 →
WordPress displays the title Not Found, followed by the statement Apologies, but no results were found for the requested Archive. Perhaps searching will help find a related post, which in turn is followed by the template tag to include a search box that lets users search your blog for more information.
WordPress uses other template files besides the main index, such as the header, sidebar, and footer templates.