Custom Divi Gallery
This is a modification of the Elegant Themes article: Moving Portfolio Module to Child Theme. Thank you Elegant Themes!
In this post you’ll learn how to move the default Divi Gallery Module to your child theme to customize it later. I needed to change the default image order to show the most recent ones as my customer uploads gallery images constantly and the Default option did not allow for this. Hope this helps!
- In the child theme folder create a new folder, for example
includes
folder. - Now copy the
Divi/includes/builder/module/Gallery.php
file into thechild-theme/includes/
folder. - Rename the file to something else, for example:
custom-Gallery.php
- Open up the
custom-Gallery.php
file and replace this code (the first line):
class ET_Builder_Module_Gallery extends ET_Builder_Module_Type_PostBased {
with:
class custom_ET_Builder_Module_Gallery extends ET_Builder_Module_Type_PostBased {
Also, replace this line:
$this->vb_support = 'on';
with:
$this->vb_support = 'off';
6. Next, open up functions.php file in your child theme folder and add the following code at the very bottom:
function custom_gallery_setup() {
get_template_part( 'includes/custom-Gallery' );
$custom_gallery = new custom_ET_Builder_Module_Gallery();
remove_shortcode( 'et_pb_gallery' );
add_shortcode( 'et_pb_gallery', array( $custom_gallery, '_render' ) );
}
add_action( 'et_builder_ready', 'custom_gallery_setup' );
Now you are ready to customize the Gallery module the way you need and keep the changes while updating / reinstalling the parent Divi theme.
Changing The Image Order To Most Recent
1. In the newly created custom-gallery.php do a search for: get_gallery (it is near line 416). Then scroll down to the $attachments_args section (near line 429).
2. Change ‘order’ from ‘ASC’ to ‘DESC’
3. Change ‘orderby’ from ‘post_name’ to ‘ID’
NOTE: This change is reflected with the Default Image Order selection in the Divi Builder. It does not create another Order option. I will work on that next.
That’s it!
If you are trying to change the order to something other than the above, WordPress has many more order and orderby parameters and you can find those here.