Bits & Babble

A blog in development.

Make WordPress Insert Ads Between Posts with the Genesis Framework

December 8, 2014  

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.

functions.php
raw download
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
  }
 
}

<?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.

2 Comments

By SPENCER SOKOL
  • About
  • Twitter
  • Studio 27

Categories

  • Alloy
  • Android
  • Appcelerator Titanium
  • Babble
  • Debugging
  • Genesis
  • iOS
  • s2Member
  • Woocommerce
  • Wordpress

About Spencer Sokol

Spencer co-founded Studio 27, a small web and application design and development company in Indianapolis. He has spent many years in both the development and testing side of the software industry, and generally avoids talking to people face to face.

Comments

  1. Paul says

    July 27, 2016 at 3:29 pm

    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

    Reply
    • Spencer Sokol says

      July 27, 2016 at 5:18 pm

      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.

      Reply

Leave a Reply Cancel reply

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

  • About
  • Indianapolis Design & Development
Copyright © 2021 Spencer Sokol

Copyright © 2021 · Bits and Babble on Genesis Framework · WordPress · Log in