2011-01-09 91 views
0
<div class="home-thumbs bottom-thumbs"> 
<?php $home_query_bottom = new WP_Query("cat=&showposts=20&offset=5"); $b = 0; ?> 
<ul class="thumbs"> 
    <?php while ($home_query_bottom->have_posts()) : $home_query_bottom->the_post(); 
     $do_not_duplicate = $post->ID; $b++; ?> 

     <li class="post-<?php the_ID(); ?> thumb"><?php get_the_image(array('custom_key' => array('thumbnail'), 'default_size' => 'thumbnail', 'width' => '160', 'height' => '160')); ?></li> 
    <?php endwhile; wp_reset_query(); $b = 0; ?> 
</ul> 
</div> 
<div id="output"></div> 
<script type="text/javascript"> 


$('.go-right').click(function(){ 

$.ajax({ 

      type: "POST", 
      url: "process_thumbs.php", 
      data: "showposts=25", 
      success: function(html){ 
       $("#output").html(html); 
      } 

}); 
}); 
</script> 

// process_thumbs.php 
    <body> 

    <?php $numposts = $_POST['showposts']; ?> 
    <div><?php echo "this is the amount of posts to be shown: $numposts"; ?></div> 
    </body> 

看起來像一個簡單的ajax調用。 .go-right確實存在,它只是在另一個文件中,我測試了點擊正在執行。這個Ajax調用基本上不起作用。也許有人可以確定我的代碼是否錯誤。我的jquery ajax不能在我的WordPress網站上工作

理想我想採取WP-查詢循環和使用Ajax與不同showposts再次運行該循環,如果有人點擊偏移。

回答

0

確保.go-right以前 jQuery的代碼或(更好的)包裝您的jQuery在

$(document).ready(function() { 
}) 

或相似,以確保處理程序是真正連接到您的.go-right元素

不要在您的process_thumbs.php中輸出<body>標籤,也許這會讓jQuery產生混淆。

檢查Firebug的控制檯/淨標籤,以確保該請求被真正被髮送。

添加一個error回調到您的.ajax,以確保您的ajax調用真正得到執行。


呀,使用絕對路徑,但有一個WordPress的輔助函數:

... 
url: '<?php echo get_bloginfo('url').'/wp-content/myplugin/mycallback';?>' 
... 

這樣一來,所有的網址將是相對於WordPress的根URL。

+0

感謝。我檢查了chrome的開發人員工具,它說我的process_thumbs.php 404錯誤。說www.url.com/process_thumbs.php,這不是網址。我想在wordpress中你需要絕對路徑? – Adam 2011-01-09 17:36:11

-2

在WordPress的(我使用的版本3.0及以上),由於與其他JavaScript庫衝突(即原型),你必須寫:的

$JQuery('.go-right').click(function(){ 

代替

$('.go-right').click(function(){ 

而且,請檢查Firebug以確保鏈接到您的Jquery庫是正確的。

一件事... WordPress的已經包括了jQuery,您可以通過書面形式得到它:

<?php wp_enqueue_script("jquery"); ?> 

<?php wp_head(); ?> 

第一行必須在第二行之前去(wp_head()),否則將無法正常工作。

0

我重寫你的代碼文檔準備好處理程序中:

$(函數(){ 您的代碼 });

它的工作好。

所以還是這是你的代碼的位置相關的問題,或者你mispell東西。

跟隨我的測試代碼:

<html> 
    <head> 
    <script type="text/javascript"> 

    $(function() { 

    $('.go-right').click(function(){ 

     $.ajax({ 

        type: "POST", 
        url: "process_thumbs.html", 
        data: "showposts=25", 
        success: function(html){ 
         $("#output").html(html); 
        } 

     }); 
    }); 

     }); 

    </script> 

    </head> 
    <body > 


    <div id="output"></div> 

    <a class="go-right">RIGHT</a> 

    </body> 
    </html> 
相關問題