Created
August 24, 2015 14:13
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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