2015-07-28 21 views
1

我試圖把WordPress的崗位值到一個新的類對象,所以我可以返回到我的Ajax postWordPress的崗位值到新的類對象

我所目前有:

阿賈克斯

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

function GetLatestBlogPost() 
{ 
    $.ajax(
    { 
     url: "IsosecWeb/php/getLatestBlogPost.php", 
     type: 'POST', 
     beforeSend: function() 
     { 
      console.log("Before send..."); 
     }, 
     success: function (successData) 
     { 
     console.log(successData); 
     console.log("successful send..."); 
     }, 
     error: function(errorData) 
     { 
      // Loading data loader 
      console.log("Error send..."); 
     } 
    }); 
} 

PHP(WordPress的獲取後的值)

require('../../blog/wp-blog-header.php'); 

// Create an object to store the data to be returned in 
$returnObject = new stdClass(); 

function GetFirstLastestBlogPost() 
{ 

    $args = array('numberposts' => 1, 'offset' => 0, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date"); 
    $postslist = get_posts($args); 
    foreach ($postslist as $post) : setup_postdata($post); 

    $returnObject->getFirstImage = getTheFirstImage(); 
    $returnObject->getBlogDate = the_date(); 
    $returnObject->getTitle = the_title(); 
    $returnObject->getContent = wp_trim_words(preg_replace("/\< *[img][^\>]*[.]*\>/i","", get_the_content(), 80), 80); 
    $returnObject->getAuthorLink = the_author_posts_link(); 

    endforeach; 

    return $returnObject; 
} 

function getTheFirstImage() 
{ 
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image'); 
    if($files) : 
     $keys = array_reverse(array_keys($files)); 
     $j=0; $num = $keys[$j]; 
     $image=wp_get_attachment_image($num, 'large', false); 
     $imagepieces = explode('"', $image); 
     $imagepath = $imagepieces[1]; 
     $thumb=wp_get_attachment_thumb_url($num); 
     echo "<img src='$thumb' class='thumbnail' />"; 
    endif; 
} 

echo json_encode(GetFirstLastestBlogPost()); 

返回控制檯消息

Before send... 

<img src='http://isosec.co.uk/blog/wp-content/uploads/2015/07/CJ9WINoWEAARSPW-150x150.jpg' class='thumbnail' />July 22, 2015EHI Awards: Finalists Announced<a href="http://isosec.co.uk/blog/?author=7" title="Posts by Jo Flynn" rel="author">Jo Flynn</a>{"getFirstImage":null,"getBlogDate":null,"getTitle":null,"getContent":"You may have read in our blog some weeks ago that we had been shortlisted for the EHI Awards 2015 in the Excellence in Mobile Healthcare category. At the time we didn\u2019t know a lot about who else was involved or the overall process but we have a lot more to share as we are now\u2026 Finalists! As you can imagine we are very excited about this, so in this blog we thought it would share a little more about&hellip;","getAuthorLink":null} 

successful send... 

它不返回與鍵值的消息。有誰知道爲什麼會發生這種情況?

預期結果

  • KEY:getFirstImage值:圖片
  • KEY:getBlogDate值:日期
  • KEY:的getTitle值:標題
  • KEY:的getContent值:內容
  • KEY:getAuthor Value:作者
Tomasz Struczyński's答案給出

輸出:

Before send... 
getBlogPost.js:26 Error... 
getBlogPost.js:27 {"readyState":4,"responseText":"July 22, 2015EHI Awards: Finalists Announced<a href=\"http://isosec.co.uk/blog/?author=7\" title=\"Posts by Jo Flynn\" rel=\"author\">Jo Flynn</a>{\"getFirstImage\":\"http:\\/\\/isosec.co.uk\\/blog\\/wp-content\\/uploads\\/2015\\/07\\/CJ9WINoWEAARSPW-150x150.jpg\",\"getBlogDate\":null,\"getTitle\":null,\"getContent\":\"You may have read in our blog some weeks ago that we had been shortlisted for the EHI Awards 2015 in the Excellence in Mobile Healthcare category. At the time we didn\\u2019t know a lot about who else was involved or the overall process but we have a lot more to share as we are now\\u2026 Finalists! As you can imagine we are very excited about this, so in this blog we thought it would share a little more about&hellip;\",\"getAuthorLink\":null}","status":200,"statusText":"OK"} 
+1

首先,我沒有看到代碼中的哪個位置打印對象 - 它只是返回。你應該做一些像'echo json_encode(GetFirstLastestBlogPost());'。第二件事,你得到的輸出可能是由'getTheFirstImage'造成的,所以你可以分享這個函數的代碼嗎? – vard

+0

@vard增加了'getTheFirstImage'函數 –

+0

@vard我也更新了它'echo json_encode(GetFirstLatestBlogPost());' - 它現在輸出的東西與它先做的不同 –

回答

2

更新 - 添加content-type頭設置

首先,你應該在這裏說明考慮使用標準的WordPress AJAX鉤https://codex.wordpress.org/AJAX_in_Plugins

至於你的方法:

在普通的PHP你有打印您的返回值。簡單地從函數返回它並不會導致它被髮送回客戶端。你必須以某種方式迴應它。我建議使用JSON格式,因爲編程很容易。

對於前端 - 添加數據類型: 'JSON' 您的要求那樣:

$.ajax(
    { 
     url: "IsosecWeb/php/getLatestBlogPost.php", 
     type: 'POST', 
     dataType: 'json', 
     beforeSend: function() 
     { 
      console.log("Before send..."); 
     }, 
    success: function (successData) 
    { 
     console.log(successData); 
     console.log("successful send..."); 
    }, 
    error: function(errorData) 
    { 
      // Loading data loader 
     console.log("Error send..."); 
    } 
    }); 

然後後端:

header('Content-Type: application/json'); 

require('../../blog/wp-blog-header.php'); 

// Create an object to store the data to be returned in 

function GetFirstLastestBlogPost() 
{ 
    $returnObject = new stdClass(); 

    $args = array('numberposts' => 1, 'offset' => 0, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date"); 
    $postslist = get_posts($args); 
    foreach ($postslist as $post) { 
     setup_postdata($post); 

     $returnObject->getFirstImage = getTheFirstImage(); 
     $returnObject->getBlogDate = the_date(); 
     $returnObject->getTitle = the_title(); 
     $returnObject->getContent = wp_trim_words(preg_replace("/\< *[img][^\>]*[.]*\>/i","", get_the_content(), 80), 80); 
     $returnObject->getAuthorLink = the_author_posts_link(); 
    } 

    return $returnObject; 
} 

function getTheFirstImage() 
{ 
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image'); 
    if($files) { 
     $keys = array_reverse(array_keys($files)); 
     $j=0; $num = $keys[$j]; 
     $image=wp_get_attachment_image($num, 'large', false); 
     $imagepieces = explode('"', $image); 
     $imagepath = $imagepieces[1]; 
     $thumb=wp_get_attachment_thumb_url($num); 
     return $thumb; 
    } 
    return null; 
} 

echo json_encode(GetFirstLastestBlogPost()); 

我已經改變了代碼造型符合標準(PSR-0及以下),因爲沒有捲曲支架的符號主要用於平板,而不是功能。

+0

我已經更新了我的問題與輸出從您的答案給出 –

+0

問題來自這個'回聲'「;'。你顯示圖像,這就是你在輸出中獲得它的原因。返回它而不是回聲它,它會好起來的。在Tomasz中查看他對「getTheFirstImage」函數的修改。 – vard

+1

沒問題。我在回答中沒有「回聲」:) –