WordPress Loop Part 2: Dig Deeper into the Loop With Template Tags

So, you have worked your way through the first part of our series on the WordPress loop! By now, you should have a good understanding of the basic functionality it provides. Let’s dig a bit deeper and find out what we can do to make our custom loop sing.

The Basic Layout:

So, we have the basic loop in place. This loop simply opens the door to new posts using the “if have posts” expression, and closes the door using the “endwhile endif” expressions.

Handy Tags (Bookmark This One):

Template tags are what you can use to alter the way that the loop displays your content. After all, in our original loop, we only tell WordPress that we would like some ice cream (or posts) however, we have yet to say anything about the flavor. That’s where template tags come in. These tags sit within your Wordpress loop markup and serve as custom content calls for anything that WordPress is able to display.

The tags shown above are simplistic in nature, and there are many more of them to understand. However, let’s analyze the tags that we have here. Our “the_title();” tag when set between perhaps a pair of “<h2></h2>” html tags would render out an appropriately sized title for your theme.  Likewise, if you were to place the “the_content” or “excerpt” tags in an area that you had a desire to see post content or a summary respectively, they would fetch that content and place them in your loop.

Note: The majority of template tags do not work outside of the loop.

Tags in action: A Working Loop

Now that we understand how tags work, I would encourage you to start experimenting with the possible combinations. There are tags to fetch just about any post or page related data that you can think of. Let’s take a look at what a full WordPress loop looks like in action.

This loop would fit very well on a “single.php” or even an “index.php” if you wanted to show full length posts. As you can see at the beginning of this block of code, we initiate the loop. Then define an area for our post details, think of it as your meta information section for the post. Within this section we declare the post title, then follow up with a list of meta information including categories, the date on which the post was written and the author. After our meta area, we follow up by displaying our post content.  At the end of the code we clean up shop by ending our while statement, then ending our if statement to complete the loop.

I hope that this article will encourage you to dive into your theme files and play around with the loop. I promise, it is not nearly as scary as you would think. Next we will dive into grabbing posts from a specific category or author. Thank you for reading. If you would like to download our demo code file, click the button below.

Demo Files

About Brock Nunn

Brock Nunn is a web designer and developer specializing in building custom WordPress for over three years. Brock operates through his company Brock Nunn Web Design offering clients personal service with outstanding results. You can follow Brock on twitter @banunn.

Leave a Reply

What is 12 + 4 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)
Start planning your project today. Get Started