Connect with us
Padiwise Banner

Others

How to Display Content for Mobile or Desktop Only Using WordPress

Published

on

How to Display Content for Mobile or Desktop Only Using WordPress

This WordPress snippet will use WordPress built-in “mobile detect” function “wp_is_mobile” to create a simple shortcode that can hide certain parts of your content from mobile visitors and-/or desktop visitors.

Did you know that you can use WordPress built-in “mobile detect” function wp_is_mobile to create a simple shortcode that can hide certain parts of your content from mobile visitors and-/or desktop visitors. The wp_is_mobile function returns true when your site is viewed from a mobile browser. Using this function you can create adaptive-/responsive WordPress themes based on visitor device.

Method 1 instructions
Simply use IF conditions in your templates.

/* Ofofonobs Developer... */<?php if( wp_is_mobile()){ ?> 
<span>mobile stuff goes here</span>
<?php } else { ?> 
<span>desktop stuff goes here</span>
<?php } ?>

Method 2 instructions
This snippet will allow you to use shotcodes the [desktoponly] desktop content [/desktoponly] or [mobileonly] mobile content [/mobileonly] to determine what content should be returned to the visitor.

Add this code to your functions.php

/* Ofofonobs Developer... */<?php 
// [desktoponly] shortcode // 
add_shortcode('desktoponly', 'wp_snippet_desktop_only_shortcode'); 
function wp_snippet_desktop_only_shortcode($atts, $content = null){ 
if( !wp_is_mobile() ){ return wpautop( do_shortcode( $content ) );
} else { 
return null;
} 
} 
// [mobileonly] shortcode // 
add_shortcode('mobileonly', 'wp_snippet_mobile_only_shortcode'); 
function wp_snippet_mobile_only_shortcode($atts, $content = null){ 
if( wp_is_mobile() ){ return wpautop( do_shortcode( $content ) );
} else { 
return null;
}
}
?>

 

Disclaimer: We sell any type of script/themes/plugins, we also offer web design/developement, graphics design, SEO, Social media followers, VTU Platform and more. visit our contact page to get in touch with us or WhatsApp (+234) 08114313795.
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Entertainment

Entertainment1 day ago

How to Get Rich as a Woman: The Ultimate Guide

Getting rich is a dream of millions of people across the United States, and building wealth starts with learning and...

Entertainment5 days ago

25 Highest Paying Jobs in America in 2024

Everyone is always looking to earn the most money possible when it comes to their hourly jobs or full-time careers....

Entertainment5 days ago

A Short-And-Simple Guide to Investments in Real Estate for Beginners

Property and real estate are considered the largest asset classes from around the world. When you own a property, the...

Entertainment5 days ago

20 of the Largest Banks in the World in 2024

In the realm of global finance, certain banking institutions stand out for their sheer size and influence. These giants shape...

Entertainment5 days ago

20 of the Largest Hotel Chains in the World in 2024

The world of hospitality is marked by remarkable establishments that offer unforgettable experiences, impeccable service, and luxurious accommodations. In this...

Entertainment5 days ago

9 Stock Trading Strategies to Try in 2024

About 20% of Americans were investing in the stock market in 2020, up 5% from 2019. It’s clear Americans are looking for...

Entertainment5 days ago

How to Know When a Good Financial Opportunity Comes Your Way

Money may not buy happiness. But according to a recent study, it can reduce your stress levels, make you feel...

Entertainment5 days ago

XRP Ripple Sec Lawsuit: What You Need to Know

As of May 2022, more than 200 individual and class action lawsuits have happened. This number means a 50% increase...

Entertainment5 days ago

What Are the Best Jobs in Mental Health Without a Degree Needed?

The job outlook for mental health careers is bright and is expected to grow 22% from 2021 to 2031. Working in...

Entertainment5 days ago

Uncovering The Truth: How Much Do You Lose Selling House As-is

When it comes to the housing market, one term you might have come across is “selling house as is”. This essentially...