How To Add A Load More Posts Button To The Divi Blog Module Tutorial by Pee Aye Creative

How To Add A Load More Button To The Divi Gallery Module

In this tutorial I will show you how to add an infinite load more button to the Divi Gallery module to click to load more images.

Join subscribers on our YouTube channel and enjoy other Divi video tutorials!

1. Add A Gallery Module And CSS Class

Add A Gallery Module

Start by adding the Divi Gallery module to your layout. 

Add A Custom CSS Class To The Gallery Module

We need to add a custom CSS class to the Divi Gallery module. Using a custom class like this will make sure that our jQuery code only affects the module with this class, rather than affecting other Gallery modules on your site.

Open the Divi Gallery module settings and go to the Advanced tab and open the CSS ID & Classes toggle. From there, copy and paste or write “pa-gallery-load-more” into the CSS Class input field, as shown in the screenshot.

add custom class to the Divi gallery module to add a load more button

Make Sure Pagination Is Off

For obvious reasons, the Show Pagination option in the Elements toggle needs to be off in the module for our load more button to work, so just double check that it is disabled.

2. Add A Button And CSS ID

Add A Button Module Below The Gallery Module

We are going to use a regular Divi button module for the load more button. This is great because it allows you to style it however you want with normal Divi settings. The only important thing is to add the button directly below the gallery module in your layout. 

Add A Custom CSS ID To The Button Module

Next, we need to add a custom CSS ID to the Divi Button module. This is needed to properly link the button and the gallery together. Take note that this is an ID, not a class.

Open the Divi Button module settings and go to the Advanced tab and open the CSS ID & Classes toggle. From there, copy and paste or write “pa_load_more” into the CSS ID input field, as shown in the screenshot.

add custom ID to the Divi button module to add a load more button to the gallery

3. Add The jQuery Snippet

Now that you have added the custom CSS class and ID to the modules, you can proceed to copy and paste the snippet below into your website, and your image gallery feed will have a functioning load more button!

Where To Add The Code Snippets

If you are using our free Divi child theme, place this snippet into the scripts.js file and remove the <script> tags at the beginning and end. Otherwise, place this in your Divi>Theme Options>Integrations tab in the “Add code to the < head > of your blog” code area.

<script>
jQuery(document).ready(function(){
    //For mobile Screens
    if (window.matchMedia('(max-width: 767px)').matches) {
        var initial_show_images = 2;
    var images_reveal = 2;
    jQuery(".pa-gallery-load-more .et_pb_gallery_item").not( ":nth-child(-n+"+initial_show_images+")" ).css("display","none");
    jQuery("#pa_load_more").on("click", function(event){
        event.preventDefault();
        initial_show_images = initial_show_images + images_reveal;
        jQuery(".pa-gallery-load-more .et_pb_gallery_item").css("display","block");
        jQuery(".pa-gallery-load-more .et_pb_gallery_item").not( ":nth-child(-n+"+initial_show_images+")" ).css("display","none");
        var images_num = jQuery(".pa-gallery-load-more .et_pb_gallery_item").not('[style*="display: block"]').length
            if(images_num == 0){
            jQuery(this).css("display","none");    
            }
        
    })
    } else {
        //For desktop Screens
        var initial_row_show = 4;
        var row_reveal = 4;
        var total_images = jQuery(".pa-gallery-load-more .et_pb_gallery_item").length;
        jQuery(".pa-gallery-load-more .et_pb_gallery_item").not( ":nth-child(-n+"+initial_row_show+")" ).css("display","none");
        
        jQuery("#pa_load_more").on("click", function(event){
            event.preventDefault();
            initial_row_show = initial_row_show + row_reveal;
            jQuery(".pa-gallery-load-more .et_pb_gallery_item").css("display","block");
            jQuery(".pa-gallery-load-more .et_pb_gallery_item").not( ":nth-child(-n+"+initial_row_show+")" ).css("display","none");
            var images_num = jQuery(".pa-gallery-load-more .et_pb_gallery_item").not('[style*="display: block"]').length
            if(images_num == 0){
            jQuery(this).css("display","none");    
            }  
        })   
    }   
})
</script>

In the above jQuery snippet there are four values where initial_show_images and images_reveal are for mobile screens and initial_row_show and row_reveal are for desktop screens. Changing these values will change the number of images that will be visible on page load, and how many images will be revealed each time you click the load more button.

4. Add A CSS Snippet

One last thing, we need to add some CSS to adjust the grid of images after clicking the load more button.

If you are using our free Divi child theme, place this snippet into the style.css file. Otherwise, place this in your Divi>Theme Options>Custom CSS code box. If you need help, check out our complete guide on Where To Add Custom Code In Divi.

/*adjust the gallery grid items after clicking the load more button*/

@media all and (max-width:980px) and (min-width: 767px) {
	.pa-gallery-load-more .et_pb_gallery_item:nth-child(3n) {
		margin-right: 0 !important;
	}
}

@media all and (min-width:981px) {
	.pa-gallery-load-more .et_pb_gallery_item:nth-child(4n) {
		margin-right: 0 !important;
	}
}

Be sure to watch the video to see all of this live and in action! Let me know in the comments if you enjoyed this!

Last updated Apr 11, 2023 @ 2:04 am

Subscribe

Each month we send out a roundup email newsletter with the latest tutorials, product updates, helpful resources, and any other industry or personal news. Occasionally we send an extra separate email here and there if we just can’t wait! So that’s what you will get if you subscribe, and you can always unsubscribe at any time if you just can’t take it anymore :)

Blog Post Optin

Please share this post!

Nelson Lee Miller (aka The Divi Teacher)

Nelson is the owner of Pee-Aye Creative in the beautiful state of Pennsylvania. He loves helping small businesses, exploring outdoors, building websites with Divi, and teaching others.

Leave A Response!

By commenting you agree to our Blog & YouTube Comments Policy

0 Comments

Submit a Comment

Your email address will not be published.

Recent Posts

0

Your Cart