Archived topic
Featured Image only on mobile?
3 replies · Started by Sandra on December 21, 2021
Hi,
I´m looking for a way to display the featured-image only on mobile view?
Under Costomizing > Layout > Featured Images there is unfortunately no setting for desktop and mobile, right? So the only thing I can do is to enable the feature image and use CSS to hide it in Desktop view. But I think this is not the best way to do it (Loading Times).
Is there any other possibility to show the featured image on mobile and deactivate it completly on Desktop?
Michael
Hi there,
its possible but not without its own issues.
We can take advantage of the GP option_generate_blog_settings filter to disable the image on single posts
https://docs.generatepress.com/article/option_generate_blog_settings/
And combine it with the wp_is_mobile() condition:
https://developer.wordpress.org/reference/functions/wp_is_mobile/
Which forms this PHP Snippet you can add to your site:
add_filter( 'option_generate_blog_settings', 'db_remove_featured_img_mobile' );
function db_remove_featured_img_mobile( $options ) {
if ( is_single() && wp_is_mobile() ) {
$options[‘single_post_image’] = false;
}
return $options;
}
Adding PHP: https://docs.generatepress.com/article/adding-php/
Things to note:
1. The end user devices may not be recognised as a mobile device by the wp_is_mobile function.
2. Your server may have Page Caching enabled. You should check with your host and whether it has separate caching for Desktop and Mobile. Not all servers have this function. Reasons for this is the Page Caching may store the page with or without the featured image.... so you may find the image loads on all devices or not at all .... best to check this carefully.
hey, is there a way to do this but have it ONLY display a featured image if NOT on mobile? that's what I need :)
Hi there,
this line of the code:
if ( is_single() && wp_is_mobile() ) {
Change to:
if ( is_single() && !wp_is_mobile() ) {