Forums › Forums › Feature Requests › Content Toggle Anchor Links › Reply To: Content Toggle Anchor Links
-
Sure. The jQuery code needs to go at the bottom of your (child) template’s source, as close to the closing </body> tag as possible.
There are WP plugins that manage this. Otherwise, you’ll need to edit your theme’s Footer.php file. Just search for </body> in the file in this case.
var headings = jQuery("h3.wp-block-ub-content-toggle-accordion-title");
This line selects all H3 headings associated with the content toggle. If you use H2 or another style, just change ‘h3’ to ‘h2’ or remove ‘h3’ altogether.
jQuery.each(headings, function (i, e) {
var w = e.innerText.split(" ")[0];
if (w.includes("’")) {
w = w.replace("’", "");
}
jQuery(e).attr("id", w.toLowerCase());
});These lines process each accordion heading, convert the first word to lower case, and add an ID attribute to the H3 element. This allows browsers to go directly to the target accordion from an anchor link (bookmark) on the same page:
*** <More information
Note: If you want to go to an accordion from another page, you need to include the full URL:
*** <More information
Finally:
var h = window.location.hash;
if (h) {
jQuery(h).trigger("click");
}These few lines get the target accordion’s ID and open the accordion when the webpage loads.