Put One Divi Accordion Module inside Another

Written by Dan Mossop

If you need to embed one Divi module inside another, there are several free plugins out there that may help, such as Simple Divi Shortcodes, Shortcodes for Divi, or Divi Library Shortcodes. They all work in more or less the same way – you save your inner module to the Divi library and these plugins generate a shortcode for that module which you can then insert in the text area of your outer module.

In theory, these could be used to display one (inner) Divi accordion module inside another (outer) one. However this does not work as expected, due to several quirks of the accordion module which are not factored in by the plugins.

In particular, the title of the outer accordion toggle ends up being overridden by the title of the last inner toggle. The toggle open icons are also missing from the inner accordion and there is some strangeness when the outer toggles are opened / closed which see the inner toggles start to animate too.

Here's an alternative to approach to embedding one accordion in another.

1) Add this PHP code to your site:

add_filter('the_content', 'dbc_process_nested_layouts', 9);

function dbc_process_nested_layouts($content) {
    $content = preg_replace_callback('/\[dbc_nested_layout id=[”"]?(\d*)["″]?\]/', 'dbc_nested_layouts_callback', $content);
    return $content;
}

function dbc_nested_layouts_callback($match) {
    if (empty($match[1])) return '';
    $id = absint($match[1]);
    $global = function_exists('et_pb_load_global_module')?et_pb_load_global_module($id):'';
    return do_shortcode($global);
}

add_action('wp_head', 'dbc_nested_accordion_css');

function dbc_nested_accordion_css() {
    echo <<<'END'
    <style>
    .et_pb_toggle_open .et_pb_toggle_close .et_pb_toggle_title:before{
        display: block !important;
        content: "\E050" !important;
    }
    .et_pb_accordion_toggling .et_pb_toggle .et_pb_toggle_close .et_pb_toggle_content { 
        display: none !important; 
    }
    </style>
END;
}

If you're using a child theme, you can put it in your child theme's functions.php file. Otherwise, the easiest way is probably to add it with a plugin such as Code Snippets.

2) Save your inner accordion module to the Divi Library.

3) Add your outer accordion to your page.
4) Place the following shortcode in one of the toggles of you outer accordion:
[dbc_nested_layout id="123"]

Where 123 is the post ID of your inner accordion.

Note, if you had previously added a shortcode from one of the plugins mentioned above, replace it with the "dbc_nested_layout" one. 

5) View the page on the front-end and you should now see your nested accordion modules, like so:

Notes

  • If you are adding the PHP code using the WP Code plugin, set the snippet's insertion location to "Run Everywhere". The option to set it to "Site Wide Header", for example, won't work as it doesn't allow the PHP code to run at the correct time during the page generation process.

We may earn a commission when you visit links on our website.

Streamline Your Divi Workflow!

Copying images and content directly from your clipboard into Divi has never been easier. Say goodbye to the cumbersome save-and-upload routine and hello to streamlined efficiency.

Latest Posts

Set Custom CSS IDs for Individual Divi Accordion Items

Assigning unique CSS IDs to specific Divi Accordion items allows for precise control over styling, targeting, and linking within your page content. This ability is particularly useful when you want to apply custom designs or create anchor links to particular accordion...

Enable Swipe Navigation in the Divi Gallery Lightbox

Enabling swipe navigation in the Divi Gallery module's lightbox allows users to seamlessly browse through gallery images by swiping left or right, creating a more interactive and mobile-friendly experience. This functionality can significantly improve user engagement...

Disable Slide-In Animation for Divi Gallery Grid Images

Control how images appear in your Divi Gallery module by toggling the slide-in animation effect for grid layouts. Disabling the slide-in animation allows gallery images to load instantly and appear statically, providing a faster and distraction-free browsing...

Control Image Count Display in Divi Gallery Lightbox

Displaying or hiding the image count in the Divi Gallery module’s lightbox can help customize the user experience, depending on whether you want to give visitors an indication of gallery progress or prefer a cleaner, distraction-free view. The ability to toggle this...

Hide Gallery Image Titles in the Divi Lightbox Overlay

Displaying image titles in the lightbox overlay of the Divi Gallery module can sometimes be distracting or unnecessary, depending on your website’s design and user experience goals. Hiding these titles creates a cleaner and more focused viewing experience for visitors...

Random Posts

About Dan Mossop

Dan is a Scottish-born web developer, now living in Brisbane with his wife and son. He has been sharing tips and helping users with Divi since 2014. He created Divi Booster, the first Divi plugin, and continues to develop it along with 20+ other Divi plugins. Dan has a PhD in Computer Science, a background in web security and likes a lot of stuff, 

7 Comments

  1. Hello, I have followed these instructions and even when I place the PHP, the first headline is replaced by the last item int eh nested accordion and the controls on the nested accordion disappear (as you mentioned was an issue for you without the code).

    Reply
    • Hey Audra, sorry to hear it isn't working for you. It looks to me like the PHP code probably isn't being run at all. I'd suggest first checking that the PHP code is where you'd expect it to be and that there are no obvious errors. Assuming that it looks okay, are you able to let me know where you've added it? It should be in, for example, the child theme functions.php file or a plugin for adding PHP (such as the Code Snippets one I mention). Thanks!

      Reply
      • Thanks for the reply, Dan.

        I had added the PHP using the Code Snippets plugin. There is an error that showed up when the code was placed: "Special characters must be escaped : [ < ] . This is at line 18, in case that is part of the issue.

        Thank you!

        Reply
        • Hey Audra, thanks for the extra info. This error suggests to me that the code has been placed in a box intended for HTML / CSS, rather than PHP. I get this exact error if I place the code in the Divi Theme Options Integrations tab (whose boxes are intended to take HTML, rather than PHP code), and in the Code Snippets plugin I get a similar (though slightly differently worded) error if I place it in the CSS tab (available in Code Snippets Pro). In Code Snippets (free or Pro), make sure you place the code in a "Functions |PHP|" tab at "WP Dashboard > Snippets > Add New". If that doesn't help, is there any chance you'd be able to send through a screenshot of your code, showing where it is added? If you send a message via the contact form, and mark it as "Bug Report", you'll get an auto-response to which you can attach the screenshot. Thanks!

          Reply
  2. Please ignore my previous comment from yesterday; I have since found a bit of script was was interfering and corrected that. Apologies for any waste of your time and thanks for providing this tip.

    Reply
    • No apologies necessary, Graham. I’m glad you got it working and I appreciate you letting me know. Thanks!

      Reply
  3. Seems this only works for the first nested accordion on a page. I have two top level accordions on a page, both of which need nested content on one of their items. The one in the first accordion works fine, the second just shows [dbc_nested_layout id="291"] rather than the inner accordion.

    See example at the link below; the inner accordion at 'Sales Methods – Pro & Cons" in the "Selling a property" accordion section works. The one at "Stamp Duty" in the "Buying a Property" one doesn't.

    Any help for making both work?

    Reply

Submit a Comment

Comments are manually moderated and approved at the time they are answered. A preview is shown while pending but may disappear if your are cookies cleared - don't worry though, the comment is still in the queue.

Your email address will not be published. Required fields are marked *.