It’s a fairly common sight for ads to show up in between individual posts in a listing of articles. If you’re using Genesis, it’s an easy process to have WordPress insert ads between posts.
For our example, let’s assume we want to dump out the same content after every fifth posts for archive, category, and search listing in our WordPress site. What we’ll need to use is the genesis_after_entry
hook. This hook is called, predictably, after every entry Genesis outputs. Armed with this knowledge, let’s take a look at our example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php add_action( 'genesis_after_entry', 'my_genesis_after_entry', 10 ); function my_genesis_after_entry() { if ( is_archive() || is_category() || is_search() ) { global $wp_query; if ( ( 0 == ( ( $wp_query->current_post + 1 ) % 5 ) ) && 0 != $wp_query->current_post ) { $interstitial = 'CONTENT GOES HERE'; // You can pull this from wherever is convenient if ( !empty( $interstitial ) ) { ?> <div class="interstitial-content"> <?php echo $interstitial; > </div> <?php } } } else if ( is_single() ) { // in case you need it } } |
On line 7 we use basic WordPress template tags for our conditional. Once we know we’re in a proper index of posts, we’ll use the current WP_Query
object to figure out where we are in the loop (line 11). We’ll use the modulo operator against five, for every fifth post, verify we’re not on the first post, and then we’ll know we can output our interstitial content.
Our content can be anything. You could randomize from a list of items, pull it from a custom Genesis settings field, or hardcode it as we’ve done in our example. For bonus points, you could setup a Genesis setting to allow the admin to alter the number of posts between each ad. While all of that is outside the scope of simply having WordPress insert ads between posts, they would be great additions to the overall concept.
Hi Spencer. This is exactly what I’m looking for. However, being a WordPress (and coding) beginner I’m not sure where to put your code. I tried putting it in both my functions.php and index.php files, but this only gets me the ‘white screen of death.’
Can you please advise as to where to place the code?
I’m using Genesis Focus Pro (HTML5) and WordPress 4.5.3.
I have Genesis Simple Hooks if that will help at all.
Thanks,
Paul
It should just go directly into your functions.php file, but as a beginner you may be doing something as simple as including the opening <?php marker when you don’t need to.