Passer la navigation

[Résolu] two seperate conditional views for mobile and desktop

This support ticket is created Il y a 4 years, 2 months. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Fuseau horaire du supporter : Asia/Karachi (GMT+05:00)

Marqué : 

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par cameronJ-3 Il y a 4 years, 2 months.

Assisté par: Waqar.

Auteur
Publications
#2315059
Screen Shot 2022-03-12 at 2.40.55 PM.jpg

Hello,

I am having trouble making a conditional view for desktop and a conditional view for mobile. I have tried using wp_is_mobile() and am not having much luck.

Do know a work around?

Thanks,
Cam

#2315945
is-mobile-conditional.png

Hi Cam,

Thank you for contacting us and I'd be happy to assist.

During testing on my website, I was able to use the "wp_is_mobile()" function, in the conditional block, by following these steps:

1. I registered a custom shortcode, that returns '0' if the "wp_is_mobile()" function returns false, and '1', if it returns true:


add_shortcode('is-mobile', 'is_mobile_func');
function is_mobile_func() {

	if( !wp_is_mobile() ){ 
		return '0';
	} else {
		return '1';
	}

}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

2. Next, I added "is-mobile" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

3. Finally, I was able to use this new custom shortcode, in the conditional block, by comparing it to a static value '1' for mobile and '0' for desktop.
( example screenshot attached )

regards,
Waqar

#2316841

Thanks Waqar, your solution Worked perfectly!