Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.
Every 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 or a WordPress website, 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 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.
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, 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.
Typically, in WordPress, you’ll see the code that runs through your latest posts using a while statement, as shown below.
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.
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.
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