Need to add responsiveness to twig file in PHP - php

I have this function in my tile.twig file that shows one post on a carousel. It works fine on portrait mode on mobile.I need it to show 2 post when it is in landscape. But I do not know how to do this in twig/PHP here is the code:
{{ fn("do_action", "render-carousel", style|default('header-carousel'), carousel_list|default("Header Carousel"), img_size|default("mobile/feed/large"), carousel_show|default(1), extra_options) }}
I need the carousel_show|default(1) to be carousel_show|default(2) when in landscape mode. Any ideas

Unfortunately, it's difficult, and not performant at all, to "tell" PHP that your device is in portrait or landscape mode, and to render different amounts of HTML accordingly.
I believe you're probably a bit stuck as you're using a plugin to render the carousel and therefore don't have control over the CSS or JS.
If you'd written the carousel yourself. You'd use media queries in CSS:
#media (orientation: landscape) {
.carousel-slide {
width: 50%:
}
}
#media (orientation: portrait) {
.carousel-slide {
width: 100%
}
}
You'd also need to tell your carousel code that you're displaying either one or two slides intially:
if (window.matchMedia("(orientation: portrait)").matches) {
Carousel.init({startingNumberOfSlides:1})
}
if (window.matchMedia("(orientation: landscape)").matches) {
Carousel.init({startingNumberOfSlides:1})
}
It sounds like you won't be able to achieve this effect with the carousel plugin you're using.
Thanks,
Jon

Related

Use different logos for mobile version

I would like to use a different logo for my mobile version.
I found out that you add another logo in your HTML source code and then define in CSS which logo is shown based on page size.
My problem is that I use Wordpress and can't really access the source code. I can only write something in the functions.php file.
My logo is places in the navigation bar, which makes it more difficult, too.
Would be soooo thankful for any help :)
Daniel
My Page
Yes you can use media queries to do that for example :
.mobile-logo-class {
display:none;
}
#media screen and (max-width: 768px) { // 768px or your break point
.mobile-logo-class {
display: inline-block; // or block
}
.desktop-logo-class {
display:none;
}
}
Or you can use the "Picture" tag but be careful for IE Support:
https://webdesign.tutsplus.com/tutorials/quick-tip-how-to-use-html5-picture-for-responsive-images--cms-21015
In normal case, if your theme provides the options for mobile logo then you can upload different logo for your mobile sections,
if there are not options for the mobile logo into your theme then you can use media queries to set the path for mobiles width
or
you can use the plugins to show different logo for your website in mobile something like this.
https://wordpress.org/plugins/rocket-wp-mobile/
STEP 1: OK first copy the code of logo in your header file which one calling the log on your desktop, copy the code and paste it below the same dive now remove the PHP code and change the div class, and give there <img src=" your image path"> and save it. you can write HTML too for image
STEP 2: Now in CSS use CSS for hiding it. something like
.logo2-img {
display: none;
}
here logo2 is your 2nd div class .
STEP 3: Now write css with media query
#media screen and (max-width: 768px) { // 768px or your break point
.logo2-img{
display: block; //
}
.desktop-logo-class {
display:none;
}
}
thats it sorry for poor English

Hide an element using PHP when loaded on mobile

I want to hide the Bootstrap carousel slider when a page is loaded on a mobile device.
Is it possible to do this using PHP?
You can parse $_SEVER['HTTP_USER_AGENT'] - but then you have to handle a lot of user-agents.
Or you can use a libary for that, for example Mobile Detect.
Have a look at this one:
https://codex.wordpress.org/Function_Reference/wp_is_mobile
or if you want something more 'complex':
http://mobiledetect.net/
However, the best solution to hide a div on mobile is css.
#media only screen and (max-device-width: 480px) {
.SliderClass{
display:none;
}
}
If you use bootstrap you can use the classes hidden-sm or hidden-xs if you don't want the carousel on mobile.
You can use jQuery
var width = $(window).width(),
height = $(window).height();
if (width < 720) {
$('.carousel').remove();
}

Can I load a different template via a media query?

I have a wordpress site and I am adjusting the css for responsiveness on different screen sizes. I changed a template that came with my theme for posts of a certain type. I would like to use the original template on smaller screen sizes. Any suggestions on how I could go about this? Thanks!
Media queries are computed by the browser, whereas templates are computed by the server, so there is no direct way to do this.
A common way though is to include the various templates on the server side and use media queries to only display the one you want.
You'd be looking at something like this:
<div class="mobile">
<?php get_template_part("content", "mobile"); ?>
</div>
<div class="desktop">
<?php get_template_part("content", "desktop"); ?>
</div>
And then in your CSS:
.desktop { display: none; }
#media (min-width: 800px) {
.mobile { display: none; }
.desktop { display: block; }
}
This will hide your desktop template on mobile, and vice versa. It will load your desktop template from content-desktop.php and your mobile one from content-mobile.php.
It's worth noting that while quick and easy, this usually isn't the best way to go about making a responsive website, because you'll be loading your content twice, and will find it a bit harder to maintain down the line. However, it's certainly a fine way to get started in making things responsive.

Server side and Client side broswer width and bootstrap carousel issue in php

I am trying to figure out a way to do this using both server and client (or maybe just client?) side code based on the browser width. What I want to do is take a carousel from bootstrap and populate the slide portion with images from the server.
This is easy to do when the browser is of one size, its a simple sql query and a loop.
But now imagine you can have an image for 1024+, and image for 768, an image for 480px and finally an image for 320px (these are all the same image, just at different widths). How would I use php and css to create a carouse that loads one image for 1024+, then once you get down to 768px it swaps that image out for one of an appropriate size, same for 480 and 320.
I haven't tried anything other then storing the images, because to be honest I am not even sure how to approach this.
How would you accomplish this?
Would it be an option to use media queries in CSS? Although content: url(...) for dynamic images aren't supported in all browsers you can play with background url() property of a div.
To give you an impression:
#media (max-width: 320px) {
#slide1, #slide2, #slide3 {
background:url("http://lorempixel.com/512/256");
}
}
#media (min-width: 320px) {
#slide1, #slide2, #slide3 {
background:url("http://lorempixel.com/1024/750");
}
}
#media (min-width: 1024px) {
#slide1, #slide2, #slide3 {
background:url("http://lorempixel.com/1920/1025");
}
}
See this fiddle in action with a fullscreen bootstrap carousell

Wordpress featured image on desktop only

I'm creating a mobile-first website that makes use of full-width background images which are uploaded to the page as featured images.
The problem I'm having is that the background images are styled inline because PHP is needed to get the actual background image. Now because they're being fetched inline, the images are going to be downloaded no matter what platform you're on. I only want the images to be available/downloaded if you're on a desktop/tablet device. NEVER on a mobile device. So I need a way that will stop the images from being downloaded if you're on a mobile device - has anyone any experience of achieving this before?
The code I currently have:
<?php $background_image = wp_get_attachment_image_src(get_post_thumbnail_id($page -> ID), 'full'); ?>
<div class="page-background-image" style="background-image: url(background_image);"></div>
You can use a library such as Mobile-Detect, which has WordPress support. Or you can use media queries in CSS:
#media (max-width: 600px) {
.page-background-image {
display: none;
}
}

Categories