This works for one file, but I’m not sure how to add the second one. Can anyone help me figure out how to include both JS files in my WordPress theme? Thanks in advance!
I faced a similar challenge when I was starting out with WordPress theme development. The good news is, it’s pretty straightforward to add multiple JavaScript files. You’re on the right track with wp_enqueue_script(). Here’s how I’d modify your existing code to include both files:
This approach allows you to enqueue multiple scripts independently. The ‘true’ at the end loads the scripts in the footer, which can improve page load times. Just make sure your file paths are correct. Also, if your dropdown-menu.js depends on my-scripts.js, you’d need to adjust the dependencies accordingly.
Adding multiple JavaScript files to a WordPress theme is quite straightforward. You’re already using wp_enqueue_script() correctly, which is the recommended method. To include both files, simply call wp_enqueue_script() twice within your load_theme_scripts() function. Here’s how you can modify your existing code:
This approach ensures both scripts are loaded properly. The ‘true’ parameter at the end places the scripts in the footer, which can improve page load performance. Remember to adjust the file paths if needed, and consider any dependencies between your scripts.