Collapsible table of contents

#897955
  • Lukasz Z
    Rank Math free

    Hello, is it possible to make the table of contents collapsible? It takes up a lot of space, and it would be very helpful if this feature were available.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello, we apologize for the issue you are facing. Yes, it is possible to make the Table of Contents by Rank Math collapsible. You can achieve this by adding a specific JavaScript code to your theme’s functions.php file and some CSS to your style.css file. Here’s how you can do it:

    Step 1: Add JavaScript Code

    Open your theme’s functions.php file and add the following code:

    
    /**
     * Convert Rank Math FAQ Block Into Accordion
     */
    function turn_rm_toc_collapsable() {
        ?>
    
            jQuery(document).ready(function() {
                var tocItems = jQuery("div.wp-block-rank-math-toc-block");
                tocItems.bind("click", function(event) {
                    var nav = jQuery(this).find("nav");
                    if (nav.css("overflow") == "hidden") {
                        nav.css("overflow", "visible");
                        nav.css("max-height", "100vh");
                    } else {
                        nav.css("overflow", "hidden");
                        nav.css("max-height", "0");
                    }
                });
            });

    Sources:
    How to Add Rank Math’s Table of Contents Block » Rank Math

    This is an automated reply from Rank Math Support Bot. Your ticket has been assigned to a human agent and we will get back to you shortly.

    Lukasz Z
    Rank Math free

    The code doesn’t work. I get this error:

    Your PHP code changes were not applied due to an error on line 240 of file …/functions.php. Please fix and try saving again.

    Unclosed ‘{‘ on line 225

    Hello,

    Thank you for contacting support.

    We don’t provide custom code snippets beyond the ones we already have in our documentation so if you would like to perform this to your TOC you can do so with the instructions here: https://rankmath.com/kb/table-of-contents-block/#make-the-table-of-contents-collapsible

    If you are having trouble implementing this please make sure to consult with your developer to help you implement the custom code.

    Don’t hesitate to get in touch if you have any other questions.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this ticket.