2013-05-09 2 views
0

어떻게 WordPress Loop에서 jquery는 어떻게 사용합니까?

<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와 나는 효과

를 반복하지 않고 워드 프레스 루프 내부에 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"); 
     });   }); 

답변

0

당신은 JS 파일에이를 추가 할 수 없습니다? 아마도 PHP 파일 안에 JS가있는 것보다 낫 겠지요. 그러면 PHP가 루프를 돌 때마다 실행되는 것을 막을 수 있습니다.

PHP 파일 내에서이 작업을 수행해야하는 경우 루프가 시작되기 전에 전역 JS 변수를 만들고 자신의 JS/jQuery 항목을 실행하기 전에이를 확인할 수 있습니다. 루프의 JS 내부에서 다시 실행되지 않도록 전역 변수를 변경하십시오.

== 편집 ==는

여기에 귀하의 주제로 별도의 JS 파일을 포함하여 몇 가지 예제 코드입니다.

이것은 당신의 functions.php에있을 것입니다 : 다음

add_action('wp_enqueue_scripts', 'front_end_js_files'); 
function front_end_js_files(){ 
    // shouldn't be needed, but to be safe 
    wp_enqueue_script('jquery'); 
    // this is looking in the same dir as your functions.php 
    wp_enqueue_script('my_own_file', get_bloginfo('template_url') . 'my_own_file.js'); 
} 

및 my_own_file.js에, 당신의 jQuery를 붙여 넣습니다. 이제 JS가 모든 페이지에 포함되므로 jQuery 선택기를 좀 더 구체적으로 만들 수 있습니다. 또는 스크립트를 대기열에 넣기 전에 PHP에서 사용자 정의 페이지 탐지를 추가하십시오.

+0

예를 들어 초보자 XD pls – RoVeR

관련 문제