Archived topic
Two featured images displaying based on browser window size
15 replies · Started by Tuukka on July 7, 2022
Hello,
I am coding a feature in GeneratePress Premium where featured image on frontpage changes in response to browser window size. There are two images: wide for desktop devices and a narrower image for mobile devices. They have different content (not only cropped).
I already got help to this problem elsewhere. The current solution is as following:
1. A desktop image set as featured image on frontpage in the standard WordPress featured image UI section,
2. An Advanced Custom Fields image field "mobile_featured_image" on frontpage which returns that image's ID and
3. The following custom code in child theme's functions.php file:
<?php
/**
* GeneratePress child theme functions and definitions.
*
* Add your custom PHP in this file.
* Only edit this file if you have direct access to it on your server (to fix errors if they happen).
*/
remove_action('generate_after_header', 'generate_featured_page_header', 10);
add_action(
'generate_after_header',
function () {
if (!is_page()) {
return;
}
$mobile_featured_image = function_exists('get_field') ? get_field('mobile_featured_image') : false;
if (!$mobile_featured_image && function_exists('generate_featured_page_header')) {
// Do the default
generate_featured_page_header();
return;
}
?>
<div class="featured-image page-header-image page-header-image-with-mobile grid-container grid-parent">
<?php
the_post_thumbnail(
apply_filters('generate_page_header_default_size', 'full'),
[
'class' => 'desktop-featured-image'
]
);
echo wp_get_attachment_image(
$mobile_featured_image,
apply_filters('generate_page_header_default_size', 'full'),
false,
[
'class' => 'mobile-featured-image'
]
);
?>
</div>
<?php
},
10
);
?>
<style>
.page-header-image-with-mobile .desktop-featured-image {
display: none;
}
.page-header-image-with-mobile .mobile-featured-image {
display: block;
}
@media ( min-width: 768px ) {
.page-header-image-with-mobile .desktop-featured-image {
display: block;
}
.page-header-image-with-mobile .mobile-featured-image {
display: none;
}
}
</style>
The code is supposed to generate two featured images but only show one based on browser window size: wide desktop image for any window sizes above 768 pixels and a narrower mobile image below that value. However, now there are three featured images inside two featured image divs. The upper div has images that change their CSS display values as determined in functions.php (display: none or display: block) but the lower div is an extra one that should not be there at all. It has the wider desktop featured image set to the frontpage. See the HTML in the following screenshot:
https://ibb.co/WzgHZWp
Also, with the code in functions.php, WordPress does not allow any image uploads, not in the ACF "mobile_featured_image" field on frontpage or even in the standard Media Library. I had to set the mobile featured image as an image already uploaded in Media Library. In addition, the featured image UI section disappers on frontpage. When the functions.php code is commented out image uploads work and the featured image UI section appears again. In that case there is only one featured image showing on frontpage: that one set on the standard WordPress featured image UI section.
Actually, I am not sure whether there should be any featured image on frontpage without the custom code in functions.php. The reason why it is there might be the earlier solution where I created an GP Premium Header Element that had the featured image in it. However, I have disabled that Header Element by setting the location value to default (none or empty) and also disabled the whole Elements GP Premium module.
Is there a GP Premium related bug in the functions.php code or in GP Premium Elements module that generates the extra featured image? Thank you in advance!
Hi there,
quick question - do you use the Block Editor ?
If so you can do this with GenerateBlocks and the Block Element.
If not then let us know a. where you want this applied - eg. posts or pages or both and b. what is featured image location for them in the Customizer > layout > blog
Hi David,
Thank you for quick response. I am somewhat familiar with the Block Editor (Gutenberg) whereas GenerateBlocks is new to me. The most important thing is that the solution works and is stable. Which approach you suggest, using GenerateBlocks or custom code? I have the newest version of WordPress so I guess GenerateBlocks should work.
I want the featured image only on page, more specifically on frontpage right below the menu spanning from the leftmost border of the browser window to the rightmost border. There is only the "Content Type" setting in Customizer > layout > blog set to "Excerpt". The other option is "Full Content".
Ok, so lets see if we can fix the issue with your code first, and if not then we'll look at the alternatives.
This line in your code:
remove_action('generate_after_header', 'generate_featured_page_header', 10);
Replace that with:
add_action( 'wp', function(){
remove_action('generate_after_header', 'generate_featured_page_header');
});
I replaced that line with the one you suggested. Now the extra featured image disappeared, also its HTML, everything! However, Media Library still does not work. Also, the featured image setting on editor UI is hidden and Advanced Custom Fields "mobile_featured_image" field visible, just like before.
OK - without code apart from CSS - you want to try this method ?
Use a Block element - Hook type:
https://docs.generatepress.com/article/block-element-hook/
Set the Hook to after_header
Set the Display Rules > Location to where you want this applied.
Using the GenerateBlocks plugin -> add an GB Image Block.
Enable the blocks dynamic data:
https://docs.generateblocks.com/article/image-overview/#dynamic-data
and set its source to Post Meta and add your custom field name in the field provided.
Then in the Blocks > Advanced > Additional CSS Classes add: hide-desktop hide-on-tablet mobile-header-image
NOTE: The hide-desktop hide-on-tablet are GP utility classes..
Now some CSS:
@media(max-width: 768px) {
.mobile-header-image + .page-header-image {
display: none;
}
}
This will hide the page-header-image on smaller screens IF there is a mobile-header-image before it in the HTML.
Thank you! I will try that method and get back to you.
Let us know how you get on.
Unfortunately that method did not work. Mobile featured image does not appear on smaller screen sizes. Desktop featured image is always there. It seems that no HTML element with the CSS class "mobile-header-image" is generated.
I think it would be easier to show my whole website to you at this point. What you think? Before I give you temporary user credentials to access the site I first have to configure my WordPress and webhotel. It might take some time.
Yes, if you can share the URL and login details ( if required ) in the Private Information field then we can take closer look
Thank you. I will share the URL and other information as soon as possible.
Ok - let us know ;)
I somehow managed to add a mobile image with CSS class “mobile-header-image” on frontpage. It is still in wrong place, inside <main> HTML element instead of right below <header> element which I want. Also, there is still the never disappearing desktop featured image.
You can inspect the site with the provided URL and login credentials (Customer Support only).
OK, so i created a Block Element in Appearance > Elements - titled Alt Mobile Hero Image
The element type is Hook and been set to: after_header
Heres the GB Image Block settings:
https://www.screencast.com/t/zeMkrD0H02
It will only appear ons screensizes upto 768px.
Just need some CSS to hide the other image now :)
The images on frontpage are finally working with your new Block Element. I added CSS to hide the desktop image. Thank you David! 🙂