2016-07-10 43 views
1

Im製作一個過濾器dropbox來顯示和更改每頁當前文章。 這裏是我的代碼:Ajax Url WordPress的參數

 <select name="order" id="orderBy"> 
     <option value="date" selected="selected">News</option> 
     <option value="orderByFeatured">Featured News</option> 
     <option value="orderByViews">Most viewed posts</option> 
     </select> 
     <select name="showposts" id="viewPostCount"> 
     <option value="12" selected="selected">Views : Default</option> 
     <option value="2">Views : 2</option> 
     <option value="3">Views : 3</option> 
     <option value="8">Views : 8</option> 
     <option value="16">Views : 16</option> 
     <option value="20">Views : 20</option> 
     </select> 

    jQuery('#orderBy,#viewPostCount').change(function() { 
    var orderBy = jQuery('#orderBy').val(); 
    var viewPostCount = jQuery('#viewPostCount').val(); 

    var targetURL = 'http://localhost/wordpress/category/tin-tuc/?order=' + orderBy + '&showposts=' + viewPostCount + ' #listPosts'; 
    jQuery('#listPosts').html('<div id="listPosts"><div class="waiting" style="height:300px;"></div></div>').load(targetURL); 
    }) 

但它不work.Look如URL例如:http://localhost/wordpress/category/tin-tuc/?order=date&showposts=5#listPosts不會work.It仍然顯示10個(默認設定WP)和我只有10個職位。 我如何使它工作?Tks爲您提供幫助!

+0

你加載頁面的全部輸出到#listPosts DIV?這根本不明智。閱讀關於在Wordpress中使用Ajax(爲此目的,有一個內置選項)。 –

+0

是的,如果URL工作,我認爲它應該顯示輸出到#listPosts div,但即使我在網絡瀏覽器上鍵入該網址,它沒有工作。 – ngocdung

回答

0

使用Ajax和WordPress的需要這裏提到要遵循一些特定的步驟:https://codex.wordpress.org/AJAX_in_Plugins

主要有以下幾點:

  1. add_action('wp_ajax_my_action', 'my_action_callback');

以上應該爲每個Ajax調用添加。

  • 在你的Ajax請求,你需要傳遞的參數:
  • var data = { 'action': 'my_action', 'orderBy': jQuery('#orderBy').val(), 'viewPostCount': jQuery('#viewPostCount').val() };

    您需要將它與其它數據一起,像你的情況,viewPostCount & orderBy。

  • AJAX的功能應該被命名my_action_callbackadd_action加入。

    function my_action_callback() { // do whatever wp_die(); // this is required to terminate immediately and return a proper response }