[Support request] Featured Image only on mobile?

Home Forums Support [Support request] Featured Image only on mobile?

Home Forums Support Featured Image only on mobile?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2055914
    Sandra

    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

    #2055931
    David
    Staff
    Customer Support

    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.

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