My wordpress site won't load the script into my page. I put the following function in functions.php:
function nav_script () {
wp_enqueue_script( 'script', get_stylesheet_directory_uri() .'/js/script.js', array('jQuery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'nav_script');
and the script containted in the scripts.js is as follows:
$(document).ready(function() {
var stickyNavTop = $('.blog-nav').offset().top;
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('.blog-nav').addClass('sticky');
}
else {
$('.blog-nav').removeClass('sticky');
}
};
stickyNav();
$(window).scroll(function() {
stickyNav();
});
});
When I view the source code of the page I don't see the script, so it didn't get loaded for some reason... I have the wp_head in the head and the wp_footer before the body ending tag, so I have zero clue of what can be happening, anyone out there can help? The function works in a normal html as I tried in JSFiddle...
Editing... Guys I achieved something by adding the script "manually" to the function, like this:
function nav_script () {
?>
<script>
$(document).ready(function() {
var stickyNavTop = $('.blog-nav').offset().top;
var stickyNav = function(){
var scrollTop = $(window).scrollTop();
if (scrollTop > stickyNavTop) {
$('.blog-nav').addClass('sticky');
} else {
$('.blog-nav').removeClass('sticky');
}
};
stickyNav();
$(window).scroll(function() {
stickyNav();
});
});
</script>
<?php
}
add_action('wp_head', 'nav_script');
But I've noticed two things:
ONE: it won't work if I don't insert the <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
CDN in the head;
TWO: It will only work if I reload the website, which is suggesting me some plugin conflict...