Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
Do you want to put your JS code into a JS file and then enqueue that in your functions.php file?
No, it must be added inline in the walker php file directly, because it includes php ID-related code for every looped menu item elements.
So it must be inside the looped PHP file, and I want for the only <script> open/close tags to render only once but the contents inside should be looped. Could I get any variables related to this walker menu item loop from the core?
my_custom_walker.php ==> it’s the looped file
class My_Walker_Nav_Menu extends Walker {
.
.
.
<script> ==> only once
jQuery {
. . . this is the code that should be run on every items here . . .
jQuery(‘#menu-item-<?php echo $item->ID; ?> …
}
</script> ==> only once
.
.
.
}