• Hi,

    I want to show adsense block after second post in main page, categories and tags.

    Do you know the code that I have to use.

    Them: Twenty Fourteen

    Thanks!!!

Viewing 7 replies - 1 through 7 (of 7 total)
  • restaurantbusinessjapan

    (@restaurantbusinessjapan)

    me too…can anyone suggest how we can modify the loop in index.php so that an adsense ad shows up after the x post? thanks…

    @javal2009

    no need to change the index or archive templates; you can do this with an added action in functions.php of the (child) theme.

    example code (virtually usable for any theme):

    function adsense_insertion_in_index_and_archives() {
    	global $wp_query;
    	if( ( is_home() || is_category() || is_tag() ) && $wp_query->current_post == 2 ) { ?>
    		<div class="advertisment">
    		YOUR ADSENSE CODE HERE
    		</div><!-- .advertisment -->
    	<?php }
    }
    
    add_action( 'the_post', 'adsense_insertion_in_index_and_archives' );
    restaurantbusinessjapan

    (@restaurantbusinessjapan)

    Thank you so so so much alchymyth….you are a php god!
    I tried many solutions from other forum posts about adding post counters, but none of them worked.
    Your solution is perfect.

    One more question: is there any way to repeat the process with different google ads? For example, have the first ad after the 2nd post, another ad after the 4th post, etc. Of course, each ad script has to be different to show a different ad.

    Thanks in advance…

    each ad script has to be different to show a different ad

    I thought google takes care of that (?)

    anyway, if you need tree different codes at three locations, try (not checked for syntax errors):

    function adsense_insertion_in_index_and_archives() {
    	global $wp_query;
    	if( ( is_home() || is_category() || is_tag() ) ) {
    	if( $wp_query->current_post == 2 ) { ?>
    		<div class="advertisment">
    		YOUR ADSENSE CODE 1 HERE
    		</div><!-- .advertisment -->
    	<?php } elseif( $wp_query->current_post == 4 ) { ?>
    		<div class="advertisment">
    		YOUR ADSENSE CODE 2 HERE
    		</div><!-- .advertisment -->
    	<?php } elseif( $wp_query->current_post == 6 ) { ?>
    		<div class="advertisment">
    		YOUR ADSENSE CODE 3 HERE
    		</div><!-- .advertisment -->
    	<?php }
    	}
    }
    
    add_action( 'the_post', 'adsense_insertion_in_index_and_archives' );
    restaurantbusinessjapan

    (@restaurantbusinessjapan)

    Works beautifully..Thank you very much…really appreciate your help ??

    Thread Starter javal2009

    (@javal2009)

    Thanks works perfectly!

    Hi, many thanks for the code.

    Just wondering what code should be added or changed to place the ads below title?

    Tried changing the_post with the_content, but the post content disappeared, although the ads was at the place where I wanted it to be. The same happened when replaced it with the_title.

    Thanks in advance ….

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Theme Twenty Fourteen] Adsense after second post in the loop’ is closed to new replies.