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
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.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.