Site logo

Archived topic

Disable Elements from AMP

8 replies · Started by stephen on June 5, 2020

Viewing posts 1–9 of 9

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.

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 );

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

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 );

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.

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

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 );

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.

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.

This archived topic is closed to new replies.