Determine whether a visitor uses a mobile phone or tablet in a WordPress topic

Source: Internet
Author: User

20140802154525
I recently added a large image on the homepage.

Although the display on the computer is good, it's a waste on the mobile phone.

Because the width is certain, the display on the mobile phone and PAD completely damages the aesthetic

So I decided to hide the image on the mobile phone/PAD.


Hide a self-written function

Self-writing function hiding is a complicated method and is not recommended. However, if you are using a version earlier than WP 3.4, you can only use this method.

1. Edit function. php of the topic and add the following code (from WP EMY) to the end)


Function is_mobile (){
$ User_agent = $ _ SERVER ['http _ USER_AGENT '];
$ Mobile_browser = Array (
"Mqqbrowser", // mobile QQ Browser
"Opera mobi", // cell phone opera
"Juc", "iuc", // uc Browser
"Fennec", "ios", "applewebKit/420", "applewebkit/525", "applewebkit/532", "ipad", "iphone", "ipaq ", "ipod ",
"Iemobile", "windows ce", // windows phone
"240x320", "480x640", "acer", "android", "anywhereyougo.com", "asus", "audio", "blackberry", "blazer ", "coolpad", "dopod", "etouch", "hitachi", "htc", "huawei", "jbrowser", "lenovo", "lg ", "lg-", "lge-", "lge", "mobi", "moto", "nokia", "phone", "samsung", "sony ", "symbian", "tablet", "tianyu", "wap", "xda", "xde", "zte"
);
$ Is_mobile = false;
Foreach ($ mobile_browser as $ device ){
If (stristr ($ user_agent, $ device )){
$ Is_mobile = true;
Break;
  }
 }
Return $ is_mobile;
}

2. Find the content you want to hide. Use the following syntax to package it

<? Php if (! Is_mobile ():?>
// Write the code that is not displayed on the mobile phone.
<? Php endif;?>

3. If you want to display some code only on your mobile phone, use the following syntax:

<? Php if (is_mobile ():?>
// Only the code displayed on the mobile phone
<? Php endif;?>
4. Complete!

Hide using the wp_is_mobile () function

WordPress provides a function for us to determine whether a mobile phone is used, that is, wp_is_mobile (). We can use it to determine whether a mobile phone is used. However, this function only supports WP 3.4 or a later version, only the above method can be used for versions earlier than 3.4.

1. Find the content you want to hide. Use the following syntax to package it

<? Php if (! Wp_is_mobile ():?>
// Write the code that is not displayed on the mobile phone.
<? Php endif;?>
2. If you want to display some code only on your mobile phone, use the following syntax:


<? Php if (wp_is_mobile ():?>
// The code displayed on the mobile phone
<? Php endif;?>

3. Complete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.