Optimizing Google AdSense

Posted on

When I implemented Google AdSense I noticed my pages loading slower. This was to be somewhat expected, but pages were stopping while each ad loaded. The sidebar was especially slow to appear.

I decided that the best solution would be to load the ads in the footer, but I was a little nervous about how best to achieve this. Luckily, I found a great tutorial on building an AdSense loader. I had to make a few tweaks to the jQuery, but it was a great starting point.

I explained in an earlier post how to display multiple AdSense ads in the loop using WP_Query properties. This builds on that functionality.

Continue reading

WordPress Tips: Post Count and Current Post

Posted on

Recently, I added Google AdSense to my site. I wanted to have an ad beneath the last post on each page, and another ad in the middle of pages with multiple posts. To do this I needed to figure out the total number of posts in the current loop. I had a hard time figuring out how to do this so I wanted to share what I learned.

The $post_count and $current_post properties of the WP_Query class provide the total number of posts in the query and the index of the current post respectively. By using these two values I was able to identify the middle post and the last post on every page.

Continue reading