I'm using WordPress 3.8.1. I want to use this function to use sticky header
<script>
$(document).ready(function() {
var $header = $("header"),
$clone = $header.before($header.clone().addClass("clone"));
$(window).on("scroll", function() {
var fromTop = $(window).scrollTop();
console.log(fromTop);
$("body").toggleClass("down", (fromTop > 200));
});
});
</script>
But it is not working for me and I don't know why. I know that jQuery is shared to my WordPress because Flexslider 2 is now working fine.
Late to the party here, but the correct solution to this is:
Understand that in WordPress, jQuery is loaded in no conflict mode. This means that the
$
variable is not referencing jQuery.But, by modifying your script to use a no-conflict-safe document ready, you can use the
$
within the function.Modify your script as follows:
Add this:
Now you can write your jQuery code like this:
Just assign the
noConflict()
function to a variable, and use that variable instead of$
seen in jQuery code.Have you tried setting the jquery file in your header (assuming you have the jquery file in your templates folder, but it can be anywhere, as long as you reference it correctly):
Example
I use jquery on all my wordpress sites and it works fine hope this helps.
Don't run your jquery in a script. This will break other scripts in wordpress.
Make a .js file and include it in your functions.php file like this.
Then in your .js file wrap your code in a No Conflict wrapper. Using $() will return undefined otherwise.