2009-05-13 8 views
0

아래 스크립트는 CMS를 사용하지 않는 데모 페이지에서 테스트했습니다. 스크립트는 index.php 파일에 있으며 잘 작동합니다.Wordpress에서 내 jQuery 스크립트가 실행되지 않는 이유는 무엇입니까?

<form method="get" action=""> 
    <input id="label_search" type="text" value="" maxlength="40" name="inputText"/> 
</form> 

<script type="text/javascript"> 
    var options = { 
     script:"includes/autosuggest.php?json=true&", 
     varname:"input", 
     minchars: 2, 
     delay: 200, 
     json:true, 
     shownoresults:false, 
     maxresults:15, 
     timeout: 5000 
    }; 

    var as_json = new bsn.AutoSuggest('inputText', options); 
</script> 

이제 Wordpress 템플릿 파일에서 같은 코드를 사용하고 싶습니다. 하지만 아무 일도 일어나지 않습니다. 스크립트가 전혀 실행되지 않는 것 같습니다.

사용자 친화적 URL을 사용 중이며/% category %/% postname %에 맞춤 퍼 뮤 링크를 설정했습니다. 그럴 수도 있습니다.

페이지로드시 파일에 대한 "alert ('hello') 테스트가 실행되기 때문에 bsn.AutoSuggest_2.1.3.js가 실행 중임을 알고 있습니다.

무엇이 잘못 될 수 있습니까?

이 내 WP 코드 :

sl_startpage.php :

<?php 
/* 
Template Name: SL - Start page 
*/ 
    get_header(); ?> 
    <div id="myArea"> 
    <?php 
    include_once('includes/storeLocator/sl_header.php'); 
    ?> 
    </div> 

<?php 
    get_footer(); 
?> 

이것은 sl_header.php에서 (simplyfied) 코드 :

<div id="sl-header"> 
    <form method="get" action=""> 
     <input id="label_search" type="text" value="" maxlength="40" name="product_search"/> 
    </form>     
    </div> 


    <script type="text/javascript"> 
    var options = { 
     script:"includes/autosuggest.php?json=true&", 
     varname:"input", 
     minchars: 2, 
     delay: 200, 
     json:true, 
     shownoresults:false, 
     maxresults:15, 
     timeout: 5000 
    }; 
    var as_json = new bsn.AutoSuggest('product_search', options); 
    </script> 

어떤 제안 누구?

플러그인 내가 사용된다 http://www.brandspankingnew.net/archive/2007/02/ajax_auto_suggest_v2.html

답변

0

좋아! 약 20 시간의 연구 끝에 나는 blog by Ryan Pharis에 대답을 발견했습니다. 의심 스럽지만 쉬운 해결책이었습니다.

Wordpress는 "사용자에게 친숙한 URL"을 사용하고 있습니다. 그러므로 내 경로는 다음과 같이 결국 : http://www.mysite.com/includes/autosuggest.php?json=true&

실제 경로는 이것이다 : http://www.mysite.com/wp-content/themes/my_theme/includes/autosuggest.php?json=true&

나는 워드 프레스의 URL을 얻을 필요가 있기 때문에 내 스크립트, 외부의 .js 스크립트에서 찾을 수 없습니다.그래서 저는 이렇게했습니다 :

<?php $site_url = bloginfo('template_url'); ?> 

<script type="text/javascript"> 
    var options = { 
     script:"<?php echo $site_url; ?>includes/autosuggest.php?json=true&", 

이제 내 스크립트가 작동하고 나는 행복한 캠프입니다.

1
includes/autosuggest.php?json=true& 

이 바로 URL입니다?

http://yourserver.com/includes/autosuggest.php 

.. 그리고 당신은 다음 두 가지에 대한 HTML을 (생성하는 템플릿을 사용 : 나는 그것이 autosuggest.php의 URL에있는 경우 귀하의 템플릿을 생성하는 파일을 기준으로 끝나게 생각 만들어 낸) URL이 : 그것은 여기에보고됩니다

http://yourserver.com/frontpage/ 
http://yourserver.com/categories/anotherpage/ 

나는 확신은 다음과 같습니다

http://yourserver.com/frontpage/includes/autosuggest.php 
http://yourserver.com/categories/anotherpage/includes/autosuggest.php 

당신은 아마보고 싶지 있을때 야 :

http://yourserver.com/includes/autosuggest.php 

. JSON의 URL 앞부분에 '/'를 추가하고 수정하는지 확인하십시오.

+0

와우, 빠릅니다. 나는 그것이 내 자신의 실수임을 발견했다. "another/dir/sl_header.php"를 사용하고있었습니다. 올바른 경로를 설정하면 모든 것이 트리거됩니다. 이제 autosuggest.php 파일을 찾지 못하는 문제 만 있습니다. bsn.AutoSuggest ('product_search', 옵션)에서 알 수 있듯이 텍스트 상자의 올바른 이름을 사용하고 있습니다. 방화 광이 방금 404 찾을 수 없음을 제공합니다. – Steven

+0

나는 다른 것을 가지고있다, 그것은 당신의 생성 된 페이지로부터 상대 URL로 해석 될 수있다. (위에서 편집을 참조) –

+0

내 URL은 http : // localhost/mywebsite/store-locator이다. 파일에 대한 "실제"경로는 다음과 같습니다. http : //localhost/mywebsite/includes/autosuggest.php. 내가 Wordpress CMS에서 사용자 친화적 인 URL을 사용하고 있기 때문에 위의 제안이 효과가 없을 것이라고 생각합니다. 한 가지 이유는 'Hello World'를 표시하는 test.php를 만들어 루트 디렉토리에 배치하면 http : //localhost/mywebsite/test.php를 통해 액세스 할 수 없기 때문입니다. Wordpress는 "Nothing found"텍스트가있는 기본 템플릿 만 표시합니다. – Steven

관련 문제