Quantcast
Channel: Bits & Babble
Viewing all articles
Browse latest Browse all 11

Make WordPress Insert Ads Between Posts with the Genesis Framework

$
0
0

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.

The post Make WordPress Insert Ads Between Posts with the Genesis Framework appeared first on Bits & Babble.


Viewing all articles
Browse latest Browse all 11

Latest Images

Trending Articles





Latest Images