Understanding WordPress Loop

Once you installed your WP blog, it’s just a matter of time before you hear this word: The Loop.

Did you install a plugin? It probably has some advanced functions to be inserted in The Loop. Do you want to insert a banner or a Google ad after the first post (or inside it)? You have to modify The Loop.

Do you want to fully display only the first five posts and show only the title for the remaining ones?

Agan, you have to modify The Loop.

It’s time to learn what is The Loop

From WP docs: The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post.

If you open your index.php file in your WP theme you will find this line of code:

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

and, later, this one:

1
<?php endwhile; endif; ?>

This is the so-called Loop. All code and HTML between these two lines will be executed at every post.

This way, every post will look the same, since it’s generated by the same loop.

What if you want to embed a Google between the first and the second post?

If you browse the web, you will find a lot of examples with multiple loops. I discourage using multiple loops because the more you write, the easier you can make mistakes.

So my suggestion is to replace

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

with

1
2
3
<?php $postcount = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $postcount++; ?>

To store in the $postcount variable the number of post displayed.

Then, just before

1
<?php endwhile; endif; ?>

This code:

1
2
3
<?php if($postcount==1){ ?>
	your google ad code here
<? } ?>

And you’ll have this result:

The Loop

This is only one example of various things you can do once you master The Loop, hope you will find it useful.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 2.92 out of 5)
Loading ... Loading ...
WordPress themes are designs for WordPress - one of the most popular blogging software nowadays.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 7 comments

  1. Mr Sun

    on August 7, 2008 at 1:56 pm

    Cool, this might come in handy when I redesign my blog.

  2. Will

    on August 7, 2008 at 5:21 pm

    This looks pretty straight forward. My have to have a go at getting that working!

  3. Kesh

    on August 7, 2008 at 8:11 pm

    who cares about word press.

  4. FayeC Web Studio :: Web Design and Development » Best Wordpress Tutorial sites

    on August 11, 2008 at 12:02 am

    [...] Understanding the loop [...]

  5. Annie

    on March 1, 2009 at 4:52 am

    How would you show more than 1 result on the search results?

    How would you show just one full post and the rest on the same blog page, headlines?

    Thanks.

  6. WordPress Loop | Theme Heven

    on April 24, 2009 at 5:27 am

    [...] Read More … [...]

  7. Marc Falk

    on May 6, 2009 at 7:39 pm

    Thanks so far..

    How will the code look like if I want to add a specific class to every second post?

    Don’t wanna write my suggestions, this form doesn’t support it?