Adding jquery to wordpress site -
i new working jquery, sorry if basic stuff.
i have been playing around on jsfiddle , got jquery want (add class parent when hover on child), when take code , try add wordpress site, not work.
this code trying add:
$('.explore > .country').hover(function() { $(this).parent().toggleclass('hover'); })
i know wordpress doesn't $ function , tried this:
<script type="text/javascript"> (function($) { $('.explore > .country').hover(function() { $(this).parent().toggleclass('hover'); }) })(jquery); </script>
here link jsfiddle
any appreciated.
be sure enqueue jquery first. comes built in wordpress doesn't come pre-activated.
search theme's function.php file "wp_enqueue", if there function there enqueuing scripts add following line it..
wp_enqueue_script( 'jquery' );
otherwise, create own enqueue scripts function..
// function enabled (enqueue) scripts in wordpress function enqueue_scripts() { wp_enqueue_script('jquery'); } add_action('wp_enqueue_scripts','enqueue_scripts');
update
alternatively
you can create js file custom code , enqueue use site wide.
create file named mycustomfunctions.js
put in it..
// describe function here jquery(function() { jquery('.explore > .country').hover(function() { jquery(this).parent().toggleclass('hover'); }) jquery('.explore > .community').hover(function() { jquery(this).parent().toggleclass('hover2'); }) });
in child theme root folder create new folder, name 'js'
put
mycustomfunctions.js
injs
folderedit child themes functions.php include..
// function enable (enqueue) scripts in wordpress function enqueue_scripts() { wp_enqueue_script('mycustomfunctions', get_stylesheet_directory_uri() . '/js/mycustomfunctions.js', array('jquery') ); } add_action('wp_enqueue_scripts','enqueue_scripts');
note - no need enqueue jquery separately we've listed dependency mycustomfunctions (see array), wordpress enqueue jquery automatically because of required dependency.
Comments
Post a Comment