Wordpress & Web Development

How To Insert PHP Content Into the Loop of Your Blog Excerpts

Ryan Dube 27-03-2013

php insert into loopEvery now and then, I get a message from the good folks at Google to my Adsense account suggesting one ad modification or another that they propose may help to bolster my ad revenue. In many cases, the suggested change is pretty simple – just post a slightly larger ad, or move it just a little bit up further “above the fold”.


However, there are times when you realize that an ad or maybe some other content needs to go into an area of your blog that isn’t so easy to get to. Sidebar content is one thing, and usually fairly easy to modify, but when it comes to other areas of your website that might be dynamically generated by code, inserting single content isn’t so simple.

For example, in the case of a WordPress blog Set Up Your Blog With WordPress: The Ultimate Guide Want to start your own blog but don't know how? Look to WordPress, the most powerful blogging platform available today. Read More or a WordPress website Forget Blogging: How To Power Any Website With WordPress Have you heard? Blogs are old news. I hope you didn’t jump on that particular bandwagon. Yep, static sites are the all the rage now. I’m joking, of course. There has always been the need... Read More , the central content of your home page is most likely a stream of excerpts from your blog posts. People can click on “Read More” to open up the post page itself. You can create a sort of a template for your blog posts to carefully insert the required ad How To Use A Wordpress Content Template To Write Faster Wordpress is a brilliant invention, and has made it possible for more and more people to have amazing websites, with beautiful themes. However, there is still the matter of the content area, which still needs... Read More in your post exactly where you want it to go, but inserting an ad into your main blog page is a little more difficult.

It’s difficult mostly because that isn’t a single flow of code that develops the page content. It’s actually a loop that goes through your most recent posts, pulls out the excerpts, and outputs the text, footer info about the article, and the “Read More” button.

Inserting PHP Content Into Your Blog Loop

So, what does that mean exactly? Well, let’s take my Adsense example. Google tells me that I’d do well if I added a third graphical ad closer to the fold. Now, the perfect location for such an ad is just slightly down the main section and to the right of the navigation bar. That’s right, dead center of the area where my blog excerpts are listed.

Ideally, the ad would go right after the first blog post excerpt, and right before the line separator between posts.


php insert into loop

With WordPress, there are a few places where this sort of post excerpt code takes place, but it really depends on your theme. Usually, you’ll see it in the “page” or “index” PHP files. Again, it depends on your theme, so your best bet is to run a simple, local web server for testing like XAMPP How To Install A Windows Web Server On Your PC With XAMPP Read More , and load your entire blog or website onto that server. Play around with the PHP file that you think is the right one and see if it changes the page.

php insert in loop

Typically, in WordPress, you’ll see the code that runs through your latest posts using a while statement, as shown below.


php insert in loop

Now, I use the Ikarus theme, which has several different layouts depending on your blog configuration. These are stored in a “layout” folder, and in my particular case, since I chose the “blog” layout, the code I’m looking for can be found in the “blog.php” file.

php insert into loop

There are a dozen and a half ways to force something to display only the first time through a while loop. Everyone is going to have their opinion, and everyone is going to feel their way is the best way. I’m not going to claim that – I’m just going to give you code that works.


Either at the start of your PHP file, or anywhere before the “While” statement, just paste the following code.

What this does is sets two variables to different values. That’s it. A is 1 and B is 2. Not equal, right? A is less than B.

So now, as you enter into the While statement, you’re going to check if A is less than B. The first time through the While loop, you know this is going to be true, so you display whatever you want to display, and then set A equal to B so that the next time through, the “A is less than B” check will no longer be true, and the thing you wanted to display only in the first time through will not be displayed again.

Here’s what that code looks like.



Now, if you think about it, you can use a similar approach to place something after the first 3 or 4 post excerpts, right? Your approach then would be to set A equal to 1, B equal to 4, and then every time through you would add 1 to A. Eventually, after 3 times through, A would equal B and your code snippet wouldn’t be executed for the rest of the times through the While loop.

Running the first example of code on my blog to insert a Google ad after only the first blog excerpt on the main page worked like a charm.

php insert into loop

Again, yet another way to do this would be to set A as a flag equal to “true” and then set it equal to “false” the first time through the loop. Like I said, six or a half dozen – one or the other. Whatever you choose to do, so long as the condition is true only the first time through the While loop, your code will work perfectly.

How do you do a php insert into a loop? How do you create such “one-time” conditions in your PHP code? Share some of your own techniques and tricks to doing this sort of thing in the comments section below.

Image Credits: PHP Coding via Shutterstock

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. MakeUseOf TechGuy
    March 28, 2013 at 1:42 pm

    That's a little convoluted - better would be to use one variable, call it $n, and then say $n++ in the loop - this increases the variable by one each time its called. Then you can use a simple if statement to say if $n==1 then do your ad thingy; you can even check multiple times to insert after every 2,5,7 articles, for instance.

    Taking it a step further, try reading in the post_content to a variable, then spitting it out one paragraph at a time using the explode() function with closing p tags as the first parameter - that will give you an array of paragraphs; you can then use the same trick to insert ads after a specific number of paragraphs within a post.

    • Ryan Dube
      March 29, 2013 at 2:11 am

      Oh that's very cool James - thanks!

    • Ryan Dube
      March 29, 2013 at 2:13 am

      Then again - wouldn't the approach used in this article reduce page load time, because you're only really running it once through the loop, rather than adding to the the variable every single time through?

      • MakeUseOf TechGuy
        March 29, 2013 at 7:44 am

        On a microsecond level...? I'm not sure, since you're defining two variables as opposed to one. But realistically no. Not unless you were running WordPress on a ZX Spectrum or something...