How do I add a jQuery script to WordPress?

Viewed 19

I am trying to use the following script in wordpress without success. Please help me how to adapt it to Warpeders and where should I put it to make it work.

$(‘table tr:nth-child(n+1):nth-child(-n+7’).addClass(‘active’);
$(‘#dates-load’).on(‘click’, function(e) {
e.preventDefault();
var $rows = $(‘.dates-prices-table tr’);
var lastActiveIndex = $rows.filter(‘.active:last’).index();
$rows.filter(‘:lt(‘ + (lastActiveIndex + 5) + ‘)’).addClass(‘active’);
});
1 Answers

If you want to include jQuery in your Wordpress theme you have to enqueue the scripts. This is done in the functions.php file within your theme folder. Here is an example:

function theme_scripts() {
    wp_register_script('jquery', get_template_directory_uri() . '/js/jquery.js', array('jquery'), '1.0.0',true); 
    wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'theme_scripts');

Note that the third parameter of the wp_register_script should be the location of your jquery script, you can enter the jquery cdn here if you intend on using that instead.

Hope this helps.

Edit: I read your question wrong. What I wrote above is to include the jQuery library in your project. If you want to actually include a custom js file with jQuery code the actions are pretty much the same. Only change the third parameter of the wp_register_script to the location of your custom js file :)

Related