Site logo

[Resolved] Disable Elements from AMP

Home Forums Support [Resolved] Disable Elements from AMP

Home Forums Support Disable Elements from AMP

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1316133
    stephen

    Do you know if there is a way to disable Elements from the AMP pages? There is exclude, I see that but AMP isn’t an option. reason I ask is because I got elements showing up that I’d use for the regular site on the AMP themed site.

    #1317046
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    You won’t find this in the Display Rules, but you can use a filter depending on the Element:
    https://docs.generatepress.com/article/generate_header_element_display/
    https://docs.generatepress.com/article/generate_hook_element_display/
    https://docs.generatepress.com/article/generate_layout_element_display/

    For example:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id ) { // Update ID to match your Element.
            if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
                $display = false;
            }
        }
    
        return $display;
    }, 10, 2 );
    #2363305
    Cheryl

    Hi,
    I’m trying the above code to hide an element on non-amp, but it’s not working. I do have the element ID in the code. Although, I am trying to hide a block element attached to the generate_menu_bar_items hook.

    This is what I’ve tried, in addition to the above:

    add_filter( ‘generate_element_display’, function( $display, $element_id ) {
    if ( 65659 === $element_id ) { // Update ID to match your Element.
    if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) {
    $display = false;
    }
    }

    return $display;
    }, 10, 2 );

    Thanks
    Cheryl

    #2363316
    Ying
    Staff
    Customer Support

    I’m trying the above code to hide an element on non-amp

    Hi Cheryl,

    The code you are using is to do the opposite, it hides the element on AMP pages. Try the below code instead:

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        if ( 65659 === $element_id ) { // Update ID to match your Element.
            if ( ! function_exists( 'is_amp_endpoint' ) && ! is_amp_endpoint() ) {
                $display = false;
            }
        }
    
        return $display;
    }, 10, 2 );
    #2364250
    Cheryl

    Hi. Thanks for the quick response. But the code didn’t work. It didn’t successfully hide the element on non-AMP.

    I also tried it with generate_element_display as this is a block element with a hook. I also tried changing false to true.

    I’ve tried it with a couple of element, but still cannot hide an element on non-amp.

    #2364308
    Cheryl

    …though I’ve found another way around this particular thing — I inserted it as an ad instead and had it just show on AMP.

    #2364422
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Just for reference, this code should do it in the future:

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 65659 === (int) $element_id ) { // Update ID to match your Element.
            if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
                $display = false;
            }
        }
    
        return $display;
    }, 10, 2 );
    #2365109
    Cheryl

    Hi,
    When I used that, it hid the item on the AMP page instead of on the non-AMP page. I’m trying to find a way to hide something on non-AMP so it only shows on AMP.

    #2365377
    David
    Staff
    Customer Support

    Hi there,

    try this:

    
    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 65659 === (int) $element_id ) { // Update ID to match your Element.
            if ( function_exists( 'is_amp_endpoint' ) && !is_amp_endpoint() ) {
                $display = false;
            }
        }
    
        return $display;
    }, 10, 2 );

    The change is subtle from: is_amp_endpoint() to !is_amp_endpoint() which will check if it is NOT an AMP page.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.