The WordPress Loop And Adding Content Outside Of It

Teli Adlam —  July 20, 2005

The WordPress loop is what controls the posts on the page. And like it’s name – it ‘loops’ the content and anything placed within the loop will be repeated until the loop ends.

Sometimes we’ll need to add content to a page that shouldn’t be repeated with every single post displayed (i.e. ads) and in order to do that, we need to know where the WordPress loop begins and where it ends.

For this example, I’ll be using the default WordPress template (Kubrick) index.php file located in the ‘[WP-INSTALL]/wp-content/themes/default/‘ folder.

You’ll see the loop starts around line 7* with:
<?php while (have_posts()) : the_post(); ?>

The give-away? The word ‘while’ – that is the element that tells you the WordPress loop is starting. You can add your information or content above this line and it will not be repeated with each post, instead, it will show before your posts start.

The loop ends around line 20 with:
<?php endwhile; ?>

The give-away? The word ‘endwhile’ – that is the element that tells you the WordPress loop is ending. You can add your information under this line and it will not be repeated with each post, instead, it will show after all of your posts.

* Technicalities:
——————————————–
Technically the loop starts at line 5 with:
<?php if (have_posts()) : ?>

However, this is simply a statement saying “if posts are available, go ahead and start ‘looping’ them” – anything you add before this will not be included in the loop, however, if you add something below it and no posts exist, whatever you added won’t be displayed either.

And technically the loop ends at line 33 with:
<?php endif; ?>

This is saying, end the ‘if posts are available…” statement from above and anything added above this line will only be displayed if there are posts, however, anything below it will be displayed regardless.

Teli Adlam

Posts

8 responses to The WordPress Loop And Adding Content Outside Of It

  1. Hi thanks for your informative post. I would like to ask how do I add adverts to my posts, in a particular way. I am not wanting to add Google ads but rather adverts from a PHP file which changes daily. I don’t want it to be included in the posting template loop because the php file will change daily with different advertisers and I don’t want the posts that were published yesterday to be updated with todays, instead I want them to keep the old ads, you get what I mean?

  2. What about if you want something after a certain amount of posts??
    I want a div to clear two posts and then go to the next line.
    read more “Need Help with WordPress Loops“.

    Thanks for your time and help.

  3. Hi Jermayn,
    In that event (i.e. counting posts within the loop), you’d need to set up your initial value, add it, then do something at a specific number.

    For instance, after <?php if(have_posts); ?>, you could add $p_count = 1; which basically sets the post count (for that loop) just before the start of the loop at 1.

    Then, just before the <?php endwhile; ?>, you add another 1 since another post is being looped through. Example: $p_count++;.

    And finally, add a bit of code which says “if this is post number -whatever-, add some special code here”. Example: <?php if(2==$p_count) {echo '<p>This is post #2</p>'} ?>

    I hope that helps out.

    ~ Teli

  4. Another note (I probably should have been more specific), you can have the “if this is post whatever, do something” code wherever you want that special code to appear. So, you can add it just before the post title, content, after the content, etc. — but it’s important that the $p_count++ part comes last.

    ~ Teli

  5. Yah :)

    Found the solution.
    What I did (which can be found here) was use the advert idea where after a certain number of posts it pulls a template file and I just added the <div class="clear"></div> into the template file.

    As stated in my comments I did try the echo but it did not work.

    Thanks for your comments :)

  6. Thanks for coming by and updating, Jermayn. I’m very glad to hear that you got the problem worked out, whichever method you used. :)

    (Though I am curious why the echo statement wasn’t working — just did a quick test and it echoed like a charm. Ah well. LOL)

    ~ Teli

  7. I would like to add a popularity plugin to my wp.org blog but am very hesitant because I don’t know much about code. It says that you need to add something “inside the loop”. Could you suggest where inside the loop it might go and in which part of my theme editor I should begin this operation?

  8. I am not a coder myself…but…I do understand these basic things… But I really appreciate that someone is actually sharing this tiny things with everyone else….it really helps…esp..for any newbie wanting to modify his/her own theme.

    I do have one question for you…How/where do u actually..add something that is outside your theme area? For instance — The empty space that I see all around your page….How do u add content/ads there?