• Resolved Lori

    (@delphine)


    Hi there!
    I’m experimenting with an amp version of my site, and have run into a little problem. Is there a way to have a specific div class show on the normal version of my site, but not be included in the amp version? Ideally it would not just be hidden, but not be sent to the amp page at all. The reason is that have an adsense unit that I want to show on the regular pages, but not on the amp pages. The amp pages are just showing a blank square.

    I think maybe some variation of the below in functions.php might be a starting point, but I’m not sure. Any advice?

    if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
    	/* Code to be removed --- what would I put here to exclude a class?*/
    }

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • @delphine Rather than create a function to remove a div for AMP URLs you could use the below conditional logic when inserting your ad. This would insert a trafitional traditional ad unit for your canonical URLs with an AMP ad unit replace your non AMP ad unit:

    <?php if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) : ?>
        <amp-ad>...</amp-ad>
    <?php else : ?>
        <div>...traditional ad unit code...</div>
    <?php endif; ?>

    There are also some plugins such as Advanced Ads that automatically convert traditional ad units into AMP ad units.

    Let me know if that works for you.

    Thread Starter Lori

    (@delphine)

    Oh gosh, you are fast! Thanks!
    I’m not sure if that would work (sorry, I’m not so great at this!).

    The ads I use get inserted by a plugin (the old “Adsense Deluxe”) via short codes. I usually insert the ads manually in the body of the post.
    This is the code I use:
    <div class="alignright"><!--adsense#red_rect--></div>

    I want them to show on the non amp pages (it’s a square at the top right of the content area) but get completely excluded from amp. So I thought making a class like .notonamp and doing something like this combined with something in functions.php would be the easiest.

    <div class="alignright notonamp"><!--adsense#red_rect--></div>

    If I’m thinking about this the wrong way, just let me know!

    And thanks again for your help!

    @delphine In that case adding a notonamp class should work. To ensure the div doesn’t display you can use a CSS solution such as the below, added to your style.css file or preferably a custom CSS plugin or theme option. This works for transitional mode.

    html[amp] .notonamp{
    display:none;
    }

    If you’re using reader mode you can apply the above within using the amp_post_template_css action.

    The previous solution should also work. From the previous snippet replace the <div>...traditional ad unit code...</div> with the snippet/shortcode you currently use, <div class="alignright"><!--adsense#red_rect--></div>.

    Thread Starter Lori

    (@delphine)

    Woo hoo, thanks James! I will experiment and post my findings here (so it will help other people too).
    All the best,
    Lori

    @delphine Sure, feel free to share. Let us know if you have any further queries on this.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exclude a div from amp pages,’ is closed to new replies.