• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Add Adsense Only in the First 2 Post of Category Page

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

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

This topic is: not resolved

Tagged: adsense, Prose

  • This topic has 2 replies, 3 voices, and was last updated 11 years, 5 months ago by tmstudiouser.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • January 18, 2014 at 10:10 pm #85839
    Manuel
    Member

    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:

    Add AdSense in Home After Second & Third Posts

    Thanks In Advanced

    January 19, 2014 at 11:50 am #85908
    Sridhar Katakam
    Participant

    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>';
    	}
    
    }

    Genesis Tutorials | Follow me on Twitter

    June 16, 2014 at 9:14 pm #110101
    tmstudiouser
    Member

    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>';
    }

    }

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble