As you have been reading up on WordPress, you may have heard or read about something called “the loop”. What is it?
The loop is a basic action that WordPress cycles through to show your posts. You, as a user, do not need to create an html page for each post. All you need to do is add a new post in the admin area of you blog. WordPress then loops through each of your posts and displays them on your site based on you theme. The loop is what makes this work.
When a single post is clicked on, WordPress dynamically creates a that single post based on the database and the theme files.
Start the Loop
When you are creating your own theme, knowing how to use the loop is essential. Without it WordPress is useless.
The loop is started by a bit of php:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
if(have_posts()) bit of this code says look and see if there are posts.
while(have_posts()) bit starts the loops. It starts to cycle though posts or pages.
the_posts() bit tells word press this is what needs to be applied to the loop.
End the Loop
Before we get into what goes into the loop, let’s take a look at how to end it.
<?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
This piece of code is placed at the end of the loop. This code tells WordPress that it has reached the end of the loop. It also tells WordPress if there are no matching posts, then to display a message.
<?php _e('Sorry, no posts matched your criteria.'); ?>
You can change this massage to say anything that you want you viewer to see if there are no relevant posts.
What goes in the Loop
Inside the loop is where you put the html code that shows how you want your post to be displayed. If you coded your theme in html first, then this is where you put the code for a single post. That is just the beginning though. How do you tell WordPress where to put the content that you input in the WordPress admin area? You use what is called WordPress template tags. These tags grab bits of information from your database and it is then displayed on the page.
There are a large number of these tags and you can find a complete list on the WordPress Codex. We will just take a look at how to use them.
If you want WordPress to display the name of the post, then you would use the template tag
You use it by putting it in php and putting it into the code where you want the title to appear. As a simple example, let’s say you want your title to show up in a h1 tag. This is how you would achieve that.
<h1><?php the_title(); ?></h1>
Now let’s say you want that title to also link to the permalink where the single post is located. You would need to add a new template tag. This tag will grab the permalink.
If you put them together you get:
<h1><a href="<?php the_permalink();>"><?php the_title(); ?></a></h1>
This may look a bit confusing, so I will reformat it for you. It look a bit more manageable broken-down.
<h1> <a href="<?php the_permalink();>"> <?php the_title(); ?> </a> </h1>
the_permalink() goes where you would usually put the url and
the_title() goes where you would usually type in the title.
This is just skimming the surface of the template tags. There are tags for the date, the author, the post class, the attachments, the id, and on and on and on. It is best to take a look at the WordPress Codex yourself to find what you need.
I hope that helps you get a handle on the basics of “the loop”. You start the loop, create you content replacing template tags for hard coded content, and then you end the loop. That is all there is to it. Happy Looping.