“The Loop” is where most of your work is done. However, it isn’t a single file. Rather, it is the part of the file which WordPress send your content to.
Here you will have your different files, archive.php
, category.php
, single.php
, etc. These different files will all have their own loop. But these files will not only have the loop, they will also be calling the header, sidebar, and footer as well.
There are lots of WordPress commands you can use within the loop to provide different sets of information about what is displayed. You can learn more about the WordPress functions at WordPress’s Codex.
Here is a sample of what might look like for an archive page:
While a Single Page might look like:
You will notice there is a lot of similarities between the two. Most of the commands in both of the pages revolve around the PHP commands called, so lets look at those.
PHP
get_header()
get_header()
– This function is used to automatically load your WordPress theme Header, the header.php file. This function comes from WordPress itself and is used to provide consistency between different WordPress theme authors. By using the same function call.
Archive Test Functions
Functions like is_day()
, is_month()
, etc. are used to determine what type of archive is WordPress looking at. You can use a series of if/else statements to code your page in a meaningful way. This way you can build a single archive page, instead of one for every possible types you might run into.
have_posts()
have_posts()
– WordPress, when it loads a page, runs a query to see what post/posts could/should be displayed. It puts these in a long list. This WordPress function is used determine if there are any posts still in that list. It returns a true/false value, so it is useful for if and while loops. Both of which we see used here.
In the archive.php page, you will notice it is called twice. First to check if there are posts at all. Often a theme developer will set up an if/them PHP conditional set. If there are no posts at all, they will display an error message, and/or include the search page. This is better than displaying nothing, as it lets the user know what is going on, and gives them something to do.
It is secondly called in the PHP while statement. In English, it would be read “While there are still posts to display do:…”.
the_post
the_post()
– This WordPress function does a couple of things “behind the scenes” for WordPress. It removes the next post out of that list we talked about previously, and places it into the current post variable. This variable is used internally, and you cannot directly access it. This shortens the list by one, so have_posts() will eventually return false.
post_password_required
post_password_required()
– This WordPress function checks to see if the post requires a password. It returns a true/false value, so it is used with a PHP if statement so, the theme author can block the content from being seen. Not all themes will have this, but if you are going to have password protected content, you will want to make sure this is something that is included in your theme.
get_sidebar
This WordPress function that is used to load the sidebar content is: get_sidebar().
This will specifically load sidebar.php. If your theme had more than one sidebar file, you will have to load the secondary sidebar via a different method.
get_footer
get_footer()
– This WordPress function that loads your theme’s footer, assuming it is in the footer.php file.
get_template_part
Sometimes you want to load the same set of code into multiple content types of the website. But you don’t want to put it into each content type, because updating/changing it could be a problem. That is where get_template_part()
comes into play.
This WordPress function is used to load an external file. You have to tell it what “part” to load. This will be the name of the file, minus the extension. So get_template_part('entry')
would load entry.php. This way you can put the part of the theme that displays the entry (post or page) and making a change to that external file, will automatically update all sections of the webpage that reference it. This makes updates more consistent and easier.
entry.php
The entry.php file is:
> '; } else { echo ' '; } ?> '; } else { echo '
'; } ?>
the_ID
The_ID is used to insert the content ID into the element. This is more useful for scripting, if you need to make use of it.
post_class
This function determines what classes are needed for the content. It adds the class=”” section, and includes the associated classes within the quotes. This would be something like class=”post-1846 post category-wordpress tag-theme entry” There are other classes that will be associated with it, but this gives you an idea. This allows you, as a theme developer an immense amount of control as to how your content is styled.
is_singular
This function is used to check to see if we are on a single post or page. If so, the content’s title is placed in a h1 tag. Otherwise, there will probably be multiple posts on the webpage, so it will use the h2 tag.
the_title_attribute
the_title_attribute
is used to write out the content’s title. This is the same regardless of if you are using a page or a post. It will automatically write the title, so you don’t have to use echo to write it out.
the_permalink
This function, the_permalink, is used to automatically write out the correct link to an article. While this is most useful on an archive page, it is standard practice to have it on all pages. Mainly because there are technically multiple links to a page, and this ensures search engines know about the correct link.
get_template_part
Notice that you can include get_template_part within another part of a template. This allows you to have smaller, more manageable files to work with, if you need to.
Developing a Theme – The Loop was originally found on Access 2 Learn