2014-09-20 13 views
1

我有一個WordPress頁面上的世界地圖。我想根據點擊地圖的哪個區域將特定的帖子加載到頁面上。我正在寫這個作爲我的網站的插件。以下是我現在有:PHP開關和AJAX顯示WordPress的帖子

的index.php:

function get_post_data(){ 

    // Switch based on region 
    switch($_REQUEST['region']) { 
    //Asia 
    case 'China': 
    case 'Japan': 
     class Post{ 
      function get_post(){ 
       global $more; 
       $more = 0; 
       query_posts('p=122'); 
       if(have_posts()) : while(have_posts()) : the_post(); 
       the_title('<h2>', '</h2>'); 
       the_post_thumbnail('medium'); 
       the_content('<p>', '</p>'); 
       endwhile; 
       endif; 
       wp_reset_query(); 
      } 
     } 
     break; 

    //Middle East 
    case 'Pakistan': 
    case 'Afghanistan': 
     class Post{ 
      function get_post(){ 
       global $more; 
       $more = 0; 
       query_posts('p=123'); 
       if(have_posts()) : while(have_posts()) : the_post(); 
       the_title('<h2>', '</h2>'); 
       the_post_thumbnail('medium'); 
       the_content('<p>', '</p>'); 
       endwhile; 
       endif; 
       wp_reset_query(); 
      } 
     } 
     break; 
    //etc 
    } 

    $post = new Post(); 
    $post->get_post(); 
    echo json_encode($post); 
    die(); 
} 

add_action('wp_ajax_get_post_data', 'get_post_data'); 
add_action('wp_ajax_nopriv_get_post_data', 'get_post_data'); 
?> 

start.js:

onRegionClick: function(element, code, region) 
{ 
    $.ajax(get_data.ajaxurl, { 
     data: {region: region, 'action': 'get_post_data'}, 
     dataType: 'json', 
     success: function(response) { 
      $("#post").html(response); 
     } 
    }); 

} 

的AJAX響應返回所有涉及到我想要的職位的標記,但它不會將它輸出到#post div。所以我知道AJAX,開關和地圖設置正確。我只是不知道如何將WP帖子分配給一個我可以用JSON輸出的變量。我認爲這與get_the_content()有關,但我不確定如何正確使用它。

回答

1

修訂如下,它應該工作:

  • Don't use query_posts。對於這種查詢,get_posts()是最好的。但是,當你拉一個後,然後get_post()就足夠了。

  • 您正在複製班級的聲明並將其埋在開關中。它應該轉到根並將帖子ID傳遞給自定義方法get_post($ID)

這將是這樣的(未經測試):

class Post{ 
    function get_post($ID){ 
     $html = ''; 
     $post = get_post($ID); 
     if($post) { 
      $html = $post->post_title; 
      $html .= wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); 
      $html .= $post->post_content; 
     } 
     return $html; 
    } 
} 

function get_post_data(){ 
    $post = new Post(); 
    $result = ''; 

    switch($_REQUEST['region']) { 

     case 'China': 
     case 'Japan': 
      $result = $post->get_post(122); 
     break; 

     case 'Pakistan': 
     case 'Afghanistan': 
      $result = $post->get_post(123); 
     break; 
    } 

    echo json_encode($result); 
    die(); 
} 

add_action('wp_ajax_get_post_data', 'get_post_data'); 
add_action('wp_ajax_nopriv_get_post_data', 'get_post_data'); 
+0

哇,讓這麼多的意義比我試圖...謝謝!現在運行得很好! – 2014-09-20 18:47:01

+1

[檢查此](http://stackoverflow.com/questions/13498959/how-to-use-ajax-in-a-wordpress-shortcode/13614297#13614297)以改善您的Ajax調用。另外,也許你可以在地圖聲明中分配帖子ID,並且調用Ajax將被簡化爲單個'get_post()'而不需要任何開關。 – brasofilo 2014-09-20 18:53:59

+0

我遇到了這個方法的一個問題:我加載的其中一個帖子在內容前總是有「數組」的字樣。它只顯示在一個,我正在加載的5個其他帖子顯示罰款...任何想法,爲什麼發生這種情況? – 2014-09-25 15:40:54