Archived topic
How to Inserts Separate Ads Above Fold on Mobile and Desktop
5 replies · Started by Theodore on October 13, 2021
Hello developer,
Please how can I insert separate ads above the fold on mobile and desktop? If possible to use a separate plugin
Hi Theodore,
Can you explain a bit more about "above the fold on mobile and desktop"?
I'm not sure what that means.
If you are looking for how to insert ads above the site header, you can use a hook element, choose generate_before_header hook.
https://docs.generatepress.com/article/hooks-element-overview/
https://docs.generatepress.com/article/hooks-visual-guide/
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.
How did you add the ads? Have you assign a location to the hook element?
If it's google absence ads, I found an article explains how to do it:
https://www.wplogout.com/insert-google-adsense-ads-in-generatepress/
Let me know :)
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/