When developing WordPress themes, it's important to know about the loop. The WordPress loop allows you to display multiple posts on a page, such as the index page of a blog. In this article, we will introduce you to the loop.

If you would like more information on creating WordPress plugins, see our tutorial series on creating your first WordPress plugin.

Starting the loop

To begin the loop, simply initiate a PHP while statement, which will run as long as there are posts available to process:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Inside the loop

Within the loop, you would call functions like the_permalink(), the_title(), the_content(), and various other WordPress functions that you want displayed for each post on the page.

In this example, we just want to display the post title with a link to the main post, as well as the post content:

<h2><a href="/support/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>

As you can see, we created a link within the title of the post on the first line, then display the content below it.

As this is a loop, it will display this information for each post that you have within WordPress until there are no more posts to process.

Ending the loop

Now that we have started the loop and added our content to the loop, we need to end it. To end the loop, you may use the following code:

<?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>

Complete WordPress loop

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><a href="/support/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

References

For further technical documentation on the WordPress loop, you may review the Wordpress Codex page on the loop.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Like this Article?

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!