How do I use jquery inside WordPress Loop? -
how use jquery inside wordpress loop without repeating effect
<div class="post"> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <a class="post_image" href="<?php the_permalink(); ?>" > <img class="post_image_home" src='<?php $thumb = get_post_custom_values('post_thumb'); echo $thumb[0]?>' alt="postimg" /> </a><!--post_image--> <?php endwhile; endif; ?></div><!--post--> jquery :
$j=jquery.noconflict(); $j('document').ready(function() { $j('.post').hover(function() { $j('.post_image').stop().animate({"margin-bottom":"10px",},"fast"); },function(){ $j('.post_image').stop().animate({"margin-bottom":"0px",},"fast"); }); });
could not add js file? better having js inside php file, , prevent running each time php goes through loop.
if must within php file, create global js variable before loop starts, , check before running own js/jquery stuff. inside js in loop, change global variable won't run again.
== edit ==
here's example code including separate js file theme.
this in functions.php:
add_action('wp_enqueue_scripts', 'front_end_js_files'); function front_end_js_files(){ // shouldn't needed, safe wp_enqueue_script('jquery'); // looking in same dir functions.php wp_enqueue_script('my_own_file', get_bloginfo('template_url') . 'my_own_file.js'); } and in my_own_file.js, paste jquery. note js included on every page, may want make jquery selector bit more specific. or, add custom page detection in php before enqueue script.
Comments
Post a Comment