how to implement wp_is_mobile on themes?

I just read the wp_is_mobile function reference and I have some questions. As you can read, there is a line explanation in bold text: It also should not be used for themes. I excactly don’t know what it’s mean. I just want to implement this in a theme which is combined with css media query for the layout and wp_is_mobile for removing some element.

For example, I want to display slider and sidebar in large window (i.e. desktop) then I want to remove them in mobile device. I can hide them with css propery display: none; visibility: hidden. But I wonder if they’re still rendered. In related to the bold line warning above, when and where I must use that function? Ok, I got an idea. How if I create a simple conditional check like this line?

<?php
if ( wp_is_mobile() )
{
    get_template_part( 'mobile-content' );
} else {
    get_template_part( 'content', get_post_format() );
}
?>

First, it checks if the mobile device detected, then my custom template will be rendered mobile-content.php. Otherwise it will render normal template content-{post-format}.php.

Please share your opinion which is the best way to implement wp_is_mobile and combine it in media query. Hope you can get my point.

Best regards.

1 Answer
1

wp_is_mobile() this function is for code the device on which you viewing site is mobile. i mean this condition will true if you view site in mobile. now as per your question you want to display slider and sidebar only in desktop then your code will be like below:

1. for sidebar

if (!wp_is_mobile() ) {
    get_sidebar();
}

2. for slider    
     if (!wp_is_mobile() ) {
            // slider related stuffs.
}

Leave a Comment