inserting ads between every 7th post of homepage and category wordpress page
-
I am trying to insert google ads between every 7th posts in my wordpress site with infinite scroll. i searched and found results but that didn’t worked for me because the WordPress theme i am using is little complex. the result i found was to add this code
<?php $postnum++; if($postnum%5 == 0) { ?> YOUR AD CODE HERE <?php } ?>
after <?php endwhile; ?> in index.php, but in my case the loop is different from the general wordpress theme. the loop i have is like this and i want to know how can i implement the above code in my case.
loop code
static function loop( $template ) { global $wp_query; echo '<input type="hidden" id="query-' . $template . '" value="' . urlencode( json_encode( $wp_query -> query ) ) . '" />'; if ( count( $wp_query->posts) > 0 ) { if( self::is_grid( $template ) ){ ?> <div class="loop-container-view grid"> <?php self::loop_switch( $template , 1 ); ?> </div> <?php }else{ ?> <div class="loop-container-view list" id="contloop"> <?php self::loop_switch( $template , 0 ); ?> </div> <?php } get_template_part('pagination'); } else { get_template_part('loop', '404'); } }
which then call this code, i made it a little shorter
static function get( $post , $template = 'blog_page' ){ $meta = meta::get_meta( $post -> ID , 'settings' ); if( isset( $meta['safe'] ) ){ if( meta::logic( $post , 'settings' , 'safe' ) ){ $classes = ' nsfw'; }else{ $classes = ' '; } }else{ $classes = ' '; } ?> <!-- post --> <article id="post-<?php echo $post -> ID; ?>" <?php post_class( 'post ' . $classes , $post -> ID ); ?>> </article> <?php }
loop switch function
static function loop_switch( $template = '' , $grid = 1 ) { global $wp_query; if ( !empty( $template ) ) { $ajax = false; } else { $query = array(); $template = isset( $_POST['template'] ) && strlen( $_POST['template'] ) ? $_POST['template'] : exit(); $query = isset( $_POST['query'] ) && !empty( $_POST['query'] ) ? (array)json_decode( urldecode( $_POST['query'] ) ) : exit(); $query['post_status'] = 'publish'; $wp_query = new WP_Query( $query ); $grid = isset($_POST['grid']) ? (int)$_POST['grid'] : 1; $ajax = true; } $template = str_replace( array( '_hot' , '_new' , '_like' ) , '' , $template ); if( $grid == 1 ){ $k = 1; $i = 1; $nr = $wp_query->post_count; if (layout::get_length(0, $template) == 940 ) { $div = 3; } else { $div = 2; } foreach ($wp_query->posts as $post) { $wp_query->the_post(); if ($i == 1) { if (( $nr - $k ) < $div) { $classes = 'class="last"'; } else { $classes = ''; } echo '<div ' . $classes . '>'; } self::grid( $post, $template ); if ($i % $div == 0) { echo '</div>'; $i = 0; } $i++; $k++; } if ($i > 1) { echo '</div>'; } }else{ foreach( $wp_query->posts as $index => $post ) { $wp_query->the_post(); if ($index > 0) { ?><!--<p class="delimiter"> </p>--><?php } self::get( $post, $template ); } } if( $ajax ){ exit(); } }
- The topic ‘inserting ads between every 7th post of homepage and category wordpress page’ is closed to new replies.