Skip to content

Instantly share code, notes, and snippets.

@shizhua
Created August 24, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shizhua/b7e38d412f5f94e9b130 to your computer and use it in GitHub Desktop.
Save shizhua/b7e38d412f5f94e9b130 to your computer and use it in GitHub Desktop.
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