Add Adsense Only in the First 2 Post of Category Page

Community Forums Forums Design Tips and Tricks Add Adsense Only in the First 2 Post of Category Page

This topic is: not resolved

Tagged: ,

This topic contains 2 replies, has 3 voices, and was last updated by  tmstudiouser 4 months, 1 week ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #85839

    Manuel
    Participant
    Post count: 8

    Hi, I tried to show only a google ad only in the first and second post in the category or in the home page.

    This is a code I tried that is from braddalton

    function wpsites_display_adsense_home() {
    if(is_home())
    echo ‘<div class=”adsense-home”><script type=”text/javascript”><!–
    google_ad_width = 468;
    google_ad_height = 60;
    //–>
    </script>
    </div>’;
    };
    add_action(‘genesis_after_post_content’, ‘wpsites_display_adsense_home’, 5 );

    How can I indicate that only execute in the first 2 step of the loop, it has a global variable than i can call to only execute in the first two post?

    Also I tried this post, but it doesnt work for me:

    http://www.studiopress.com/forums/topic/add-adsense-in-home-after-second-third-posts/

    Thanks In Advanced

    #85908

    Sridhar Katakam
    Participant
    Post count: 1050

    This is working fine.

    add_action( 'genesis_after_entry', 'google_ad' );
    function google_ad() {
    
    	if ( ! ( is_home() || is_category() ) )
    		return;
    
    	global $wp_query;
    	if(($wp_query->current_post == 0) || ($wp_query->current_post == 1)) {
    		echo '<div class="adsense">Adsense code here</div>';
    	}
    
    }

    #110101

    tmstudiouser
    Participant
    Post count: 16

    Sridhar, I have the same question as above but want to use this only on category page and not home page.

    I added your code to the php file but I could actually see the code at the top of the page. I changed it to display the ad on cat13. Can you look at it and tell me where I went wrong, please. Thanks!

    add_action( ‘genesis_after_entry’, ‘google_ad’ );
    function google_ad() {

    if ( ! ( is_category(13) ) )
    return;

    global $wp_query;
    if(($wp_query->current_post == 0) || ($wp_query->current_post == 1)) {
    echo ‘<div class=”adsense”>Adsense code here</div>’;
    }

    }

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.