Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
insert ad codes after first, second or nth post. http://wpsites.org/?p=10364/
<?php
function insert_between_posts( $post ) {
global $wp_query;
// Check if we're in the right template
if ( ! is_home() )
return;
// Check if we're in the main loop
if ( $wp_query->post != $post )
return;
// Check if we're at the right position
if ( 1 != $wp_query->current_post )
return;
// Display the banner
echo '<div>Some banner</div>';
}
add_action( 'the_post', 'insert_between_posts' );
?>
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<?php if ( have_posts() ) : $count=0; ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); $count++; ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php if ($count == 2) : ?>
Ad Codes after 2nd post
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php if ($wp_query->current_post == 1) : ?>
Ad Codes after 2nd post
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<?php if ($wp_query->current_post == 1) : ?>
Ad Codes after 2nd post
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment