Site logo

Archived topic

How to Inserts Separate Ads Above Fold on Mobile and Desktop

5 replies · Started by Theodore on October 13, 2021

Viewing posts 1–6 of 6

Hello developer,

Please how can I insert separate ads above the fold on mobile and desktop? If possible to use a separate plugin

Thanks for the swift response. What I mean is I want to display ads under the site menu targeting different ads for mobile and PC. With the information provided above, I went for generate_after_header but the ads didn't display.

Hi,

Thanks for this guide. That was what I did. But I used the shortcode to restrict the ads code only to mobile screen.

But it didn't work.

Ah, in this case, I would recommend to add mobile ads to a hook element and add non-mobile ads to another hook element.

Then use this PHP snippet to choose different hook element to be displayed on different devices.

For example:

1. For elements that display on none-mobile screen:

if ( wp_is_mobile() ) {
add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    $id_targets = array(100, 101, 102);
  if ( in_array($element_id, $id_targets )) {
        $display = false;
    }

    return $display;
}, 10, 2 );
}

2. For elements that display on mobile screen:

if ( !wp_is_mobile() ) {
  add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    $id_targets = array(200, 201, 202);
  if ( in_array($element_id, $id_targets )) {
        $display = false;
    }

    return $display;
}, 10, 2 );
}

You just need to replace the element IDs $id_targets = array(200, 201, 202);with your own.

You can find the ID number in the element editor, for example, in the below screenshot, the ID is 182.
https://www.screencast.com/t/tR2zIQt6BnTk

You can add the PHP snippet using one of the method mentioned in below article:
Adding PHP: https://docs.generatepress.com/article/adding-php/

This archived topic is closed to new replies.