Disable WordPress Sidebar in mobile view conditionally

I would like to disable my WP sidebar from within the template using something like the below, though I want to also conditionally disable it. Condition would be…when device width is max X, i.e. max-device-width: 720, or when pagination is on.

<?php if (wp_is_mobile() )
    {
    //do nothing
}
    else
    {
    < ? php get_sidebar(); ? >;
}
?>

The reason for the condition…on some mobiles (tablets), the sidebar is still present, not paginated, so I want it to be shown.

Related posts

Leave a Reply

1 comment

  1. You can achieve this with CSS media queries. Put something like this in your style sheet:

    @media (max-width: 720px) {
        .wp_sidebar {
            display: none;
         }
    }
    

    And the sidebar will disappear if the page is 720px or less. I’d recommend against doing the mobile check on the server side, if that’s a situation you can avoid.