There is a function called wp_is_mobile that provides a condition to display content only when the user is viewing on mobile. The function returns true when the site is loaded on mobile. Use it like this:
<?php if( wp_is_mobile()){ ?>
// mobile stuff goes here
<?php } else { ?>
// desktop stuff goes here
<?php } ?>Note: Make sure your caching mechanism caches separately for mobile and desktop. You can do this for example with the WP-Rocket plugin.