我試圖把WordPress的崗位值到一個新的類對象,所以我可以返回到我的Ajax post
WordPress的崗位值到新的類對象
我所目前有:
阿賈克斯
$(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…","getAuthorLink":null}
successful send...
它不返回與鍵值的消息。有誰知道爲什麼會發生這種情況?
預期結果
- KEY:getFirstImage值:圖片
- KEY:getBlogDate值:日期
- KEY:的getTitle值:標題
- KEY:的getContent值:內容
- KEY:getAuthor Value:作者
輸出:
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…\",\"getAuthorLink\":null}","status":200,"statusText":"OK"}
首先,我沒有看到代碼中的哪個位置打印對象 - 它只是返回。你應該做一些像'echo json_encode(GetFirstLastestBlogPost());'。第二件事,你得到的輸出可能是由'getTheFirstImage'造成的,所以你可以分享這個函數的代碼嗎? – vard
@vard增加了'getTheFirstImage'函數 –
@vard我也更新了它'echo json_encode(GetFirstLatestBlogPost());' - 它現在輸出的東西與它先做的不同 –