I’ve written a short tutorial in the past about being able to add Adsense or any other content between posts in WordPress. The Genesis theme framework by StudioPress is a bit different. The concept is not all that different, but applying the method requires knowing a little PHP and the hooks to the Genesis theme framework.
Before getting into the code, I want to make a big shout out to Phil McDonnell for helping me wrap my head around this code. The following resources were available, but none worked as Genesis 2.0 changed a bit since implementing HTML 5.
Adding Content Between Posts In Genesis
Essentially in a list of posts, we will have several loops. We want our code to be between the posts, but not cut out of the loop where it may not display properly. With the HTML5 markup that is in Genesis, we don’t want to use the hook genesis_after_post for, but the HTML5 hook for genesis_after_entry. Check out the Genesis Visual Markup Guide to see where it will fit.
We also want to put a loop counter in place so the code will automatically print after so many posts. Lastly, we need to tell this code how many posts should display before the content or advertisement will be shown.
<pre class="lang:default decode:true " >// Put text into the loop but only after every third post add_action( 'genesis_after_entry', 'themeprefix_between_post_content' ); function themeprefix_between_post_content() { global $loop_counter; $loop_counter++; if( $loop_counter == 3 ) { echo 'This will print every third post!'; $loop_counter = 0; } }</pre>
Don’t forget to change the themeprefix in the above code to whatever text domain you have specified for your theme.
Please note, this is a counter after so many number of posts, and not on specific posts… like after post 2 and post 7. It is an equal amount of posts. Also, if you’re using this to display Adsense, please make sure you’re up-to-date on how many display spots are allowed on a single page. If you use more than what the rules permit, then you may end up suspended from Adsense. (Note: As for December 2013, the max amount of Adsens slots that can be displayed per page is 3.)
You can also replace the printed area (the line with the term ‘echo’) with a widget so you can edit that area anytime with whatever content you want.
Adding Content Between Posts In Genesis – Adding A Widget
1. You need to register a new widget in your theme’s functions.php file
//* Register Between Post Content Widget genesis_register_sidebar( array( 'id' => 'between-post-content', 'name' => __( 'Between Post Content', 'theme-prefix' ), 'description' => __( 'This is the widget between every few posts.', 'theme-prefix' ), ) );
2. Use the code above and add the widget to it. We want the widget content to only appear if content has been dragged into the widget via Appearance> Widgets.
//* Add widget to after entry to output every 3 posts add_action( 'genesis_after_entry', 'themeprefix_between_post_content' ); function themeprefix_between_post_content() { global $loop_counter; $loop_counter++; if( $loop_counter == 3 ) { if ( is_active_sidebar( 'between-post-content' ) ) { echo '<div class="between-post-content widget-area"><div class="wrap">'; dynamic_sidebar( 'between-post-content' ); echo '</div></div><!-- end .top -->'; } $loop_counter = 0; } }
We want to use the class for wrap as that is what most of the sidebars in Genesis 2.0 are using since implementing HTML5.
3. Add style to your themes style sheet for the .between-post-content.
Pretty cool trick, huh? 🙂
Shiv Saroya says
Yes, I realized that was the better way of doing it. So I modified the post. Glad to help.
Gautam says
Great tutorial,one can add adsense Ads between posts and add any type of widget too.This tutorial will really help genesis users including me.
George says
For adding adsense ads you can use plugin named ad injection. I dont its possible to add widget in between posts.
Jenny says
hmm this looks handy. gonna have to save this for when i figure out my new theme… IF i ever do :\
Zimbrul says
What I like in Genesis is that you can add “bits” pretty much anywhere and if you are a master of CSS you end up with pretty cool effects.
Vinay says
Great post..Iam looking for this info for a long time and thanks for the article
Dr. Erica Goodstone says
Nile,
Very interesting. Does this type of code work with other themes?
Since I have never used the php, I would not know how to get in there to add the code.
But it is really cool to be able to have your ad pop up, I am assuming it shows at the bottom or on the sidebar at the scheduled interval between posts. Or does it appear as a popup?
Warmly,
Dr. Erica
Rebekah Radice says
Thank you, thank, you, thank you for this Nile! I have been trying to find an answer to this exact question. I, like you, just couldn’t wrap my head around it.
You always seem to have just the answer I need!
Pankaj says
I am using Genesis on few of my blogs. And this tutorial is really helpful for me. I will use these steps.
Nick G says
Hello, the code works beautifully, but for some reason on my mobile site it shows the widget between every single post on front page as opposed to just the very first post. Do i need to modify the code in some way to accommodate this?
Nile says
It shouldn’t do that… because it works with the loop count unless you left the number at 1… then it will. I suggest if you do display 10 posts to a page, then you think about making the number every 3 posts or so. In mobile, you need to add in the style.css under media query that the area is hidden.
Big F says
I have taken similar code from several sites to modify the genesis loop counter and tried to apply default loop on my site. They all output the widget after each post however and seem to ignore the loop counter. Did you solve this and can you give me a hint Nick? Pretty frustrating stuff.
Danny says
Hello! Great tutorial.
I can apply in the Child Theme Genesis ? I’m using Focus Pro Child.
Nile says
Yes, you definitely can apply this to that.
Saurab Parakh says
In my opinion stuff like ads between posts irritates the article reader because it is a kind of irrelevant stuff in between. Btw while I started my blog a couple of years back, I too was very exited about putting the ads in between.