Docs    Instagram    Customizations

Display the Instagram Feed in a Carousel layout only on mobile devices

How to keep a grid layout on desktop and switch to a carousel on smaller screens, in the Pro version.

Would you like to show your Instagram feed as a regular grid on desktop and as a carousel on mobile devices?

The plugin has no setting that switches the layout by device, so we’ll turn on the Carousel layout and add a short snippet that turns it back into a grid on larger screens.

Before you get started, make sure Instagram Feed Pro is installed and activated on your site.

Requirements: The Carousel layout is part of Instagram Feed Pro at every license level, from Basic up. The free Instagram Feed plugin doesn’t include it.

Here is what we will cover.

To get started, we’ll switch your feed to the Carousel layout in the feed editor.

If you only want a different number of columns on each device, you don’t need a snippet. The Columns setting in the same Feed Layout section has separate Desktop, Tablet, and Mobile values. The snippet below is for changing the layout itself, from a carousel to a grid.

  1. From your WordPress admin sidebar, go to Instagram Feed » All Feeds and click the name of the feed to open it.
  2. Next, click the Customize tab and open the Feed Layout section.
  3. Under Layout, select Carousel.
  4. Then, click Save.

For more on the carousel’s own settings, such as rows, arrows, and autoplay, see our guide on creating an Instagram carousel.

Next, we’ll add the snippet that turns the carousel back into a grid on desktop.

Adding the Snippet in WPCode

Now we’ll add the code with WPCode, which keeps your custom code in one place. If you’ve never used it with the plugin, see our guide on how the WPCode integration works.

The plugin sets up the carousel while each feed loads, and it announces that step with an event named sbiafterlayoutinit. The snippet listens for that event and, on screens wider than 480 pixels, changes the feed’s layout back to a grid before the carousel starts. That is why the snippet goes in the site-wide footer and doesn’t go inside the sbi_custom_js function. That function runs after the feed has loaded, which is too late.

First, here is the code to paste. You don’t need to add script tags, because WPCode adds them for you.

jQuery(window).on('sbiafterlayoutinit', function (event) {
   if (event.feed.layout === 'carousel' && jQuery(window).width() > 480) {
      event.feed.layout = 'grid';
      jQuery(event.feed.el).find('.sbi_carousel').removeClass('sbi_carousel').css('display', '');
   }
});
  1. Install and activate WPCode, then go to Code Snippets » Add Snippet.
  2. Hover over the box named Add Your Custom Code (New Snippet) and click + Add Custom Snippet.
  3. In the window that opens, choose JavaScript Snippet.
  4. Next, type a title, such as Carousel only on mobile. Then paste the code above into the code box.
  5. Then, find the Location field and choose Site Wide Footer.
  6. At the top of the page, switch the snippet from Inactive to Active. Then click Save Snippet.

Visitors on a screen 480 pixels wide or narrower see the carousel. On wider screens, the snippet removes the carousel and the feed shows as a grid. The 480-pixel line is the same one the plugin’s own stylesheet uses for its mobile styles. To move it, change the 480 in the code.

The snippet checks the screen width when the feed loads. It only changes feeds that are set to the Carousel layout, and it changes every one of them on your site.

Next, we’ll look at adding the same code to your theme instead.

Adding the Snippet in functions.php

If you’d rather not use WPCode, you can print the same script from your theme’s functions.php file. Add the code below to the end of the file. If you edit a theme that gets updates, use a child theme so an update doesn’t remove your code.

function sb_custom_carousel_grid() {
	?>
	<script>
		jQuery(window).on('sbiafterlayoutinit', function (event) {
			if (event.feed.layout === 'carousel' && jQuery(window).width() > 480) {
				event.feed.layout = 'grid';
				jQuery(event.feed.el).find('.sbi_carousel').removeClass('sbi_carousel').css('display', '');
			}
		});
	</script>
	<?php
}
add_action( 'wp_footer', 'sb_custom_carousel_grid' );

The wp_footer action prints the script in the footer of your pages, so it works the same way as the WPCode snippet.

That’s it! Your feed now shows as a carousel on mobile devices and as a grid on larger screens. To change how the carousel itself looks, see our guide on creating an Instagram carousel.

Frequently Asked Questions

Below, we’ve answered some of the most common questions about showing the carousel only on mobile.

Can I use this snippet with the free Instagram Feed plugin?

No. The free plugin doesn’t include the Carousel layout, and it doesn’t run the sbiafterlayoutinit event the snippet listens for.

Can I use carousel=true in the shortcode instead of the feed editor setting?

Not for a feed made in the feed editor. A feed made there reads only a short list of shortcode options, and carousel isn’t one of them. Instead, choose Carousel under Feed Layout in the editor.

The carousel still shows on my desktop. What should I check?

Make sure the snippet is set to Active and that its Location is Site Wide Footer. Also make sure the code isn’t inside the sbi_custom_js function. Code there runs after the feed has loaded, which is too late to stop the carousel.

support

Couldn’t find your answer in the docs?

Contact Support