- This topic has 5 replies, 2 voices, and was last updated 3 years, 11 months ago by
Fernando.
-
AuthorPosts
-
April 20, 2022 at 4:35 pm #2195331
Mario
Hi,
I want to do the following:
Add an image at the top of the content on mobiles and hide it on desktop
Add the same image on desktop but a little lower than the one on mobiles and hide it on mobiles
I found a way to do it by using hide-on-desktop and hide-on-mobiles classes, but isn’t there a better or easier way?
What i dislike the most about this manner is that it might load both images even though its only showing one?
Thanks,
MarioApril 20, 2022 at 6:41 pm #2195375Fernando Customer Support
Hi Mario,
Using the
hide-onclasses would be the easy and most accurately working way to do this.If however you want to it not to load totally, you can try adding it through a Block Element, and then utilize the
generate_element_displaydisplay filter.Reference: https://docs.generatepress.com/article/generate_element_display/
For instance:
add_filter( 'generate_element_display', function( $display, $element_id ) { if ( 393346 === $element_id && wp_is_mobile() ) { $display = false; } return $display; }, 10, 2 );Kindly replace
393346with the id of the Element. With this code, the Block Element displays on Desktop and not on mobile. You’ll have to create another Block Element for the other image.Getting the ID: https://athemes.com/tutorials/wordpress-page-id-and-post-id/
You can compare afterwards if
hide-onor thegenerate_element_displayis faster through website speed tests probably if the change is noticeable.Hope this helps! 🙂
April 20, 2022 at 8:14 pm #2195430Mario
Hi Fernando,
Sounds like a good alternative to the hide classes option.. Thanks you 🙂
I am wondering if the following could work as well?
The image i am trying to do this with is the featured image. so what if i enable the featured image option to show on posts (featured images are turned off at the moment cause i manually inserted them) and you provide me CSS to only display the featured images on mobiles.
I could then set the images i manually inserted to only show on desktop with the generate_element_display display filter option.
Would that work?
Thanks,
MarioApril 20, 2022 at 8:58 pm #2195451Fernando Customer Support
Yes, that would work, but to your purpose of have only one image element, you’ll still have two on Desktop, one of them just hidden.
If you want this, you can try this CSS:
@media (min-width: 769px) { .featured-image { display:none; } }Hope this clarifies. 🙂
April 21, 2022 at 11:18 am #2196301Mario
Thanks Fernando! 🙂
April 21, 2022 at 5:09 pm #2196468Fernando Customer Support
You’re welcome Mario! Glad to be of assistance! Feel free to reach out anytime you’ll need assistance with anything else. 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.