The WordPress Loop

I often get asked about “the loop” in WordPress, as many of my plugins require knowledge of it for successful integration.

“The loop” is simply some WordPress code that handles the fetching of post or pages. Each WordPress theme has a number of files that handle different types of output – for instance, index.php is used for displaying a number of posts, page.php displays pages and single.php shows a single post. Each of these contains the code for the loop.

The first line is usually this…

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

And the last line is usually…

<?php endwhile; ?>

Everything in between is the setting up and displaying of the post/page in question.

So, let’s use Simple Social Bookmarks as an example. You will probably want to put the function call for this just before that last line of the loop – that will it will be the last thing displayed after each post or page. However, you may want other content to appear first, in which case you will choose a different location between the above 2 lines to put it.

What I can’t tell you is exactly where you need to put the code – that’s entirely down to you and how you wish the output. Indeed, there’s no reason why, in the case of Simple Social Bookmarks, you have to put it in the loop – you could have one at the top of each page. Bear in mind, however, that if you put code outside of the loop then it may not be able to get certain information – in out example, you’d have to supply a URL, as normally this is supplied by the loop to indicate the current post/page.

I hope this makes sense. If not, you can read more about it at WordPress.org.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading