我试图将WordPress帖子值放入新的类对象中,以便我可以返回Ajax post
我目前得到了什么:
的Ajax
$(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...
它没有使用键值返回消息。有谁知道为什么会这样?
预期结果
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…\",\"getAuthorLink\":null}","status":200,"statusText":"OK"}
答案 0 :(得分:2)
更新 - 添加内容类型标头集
首先,你应该考虑使用标准的Wordpress AJAX挂钩,如下所述:https://codex.wordpress.org/AJAX_in_Plugins
至于你的方法:
在普通PHP中,您必须打印返回值。简单地从功能中返回它并不会导致它被发送回客户端。你必须以某种方式回应它。我建议使用JSON格式,因为它很容易编程。
对于前端 - 添加dataType:&#39; json&#39;对你的要求:
$.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及以下),因为没有卷曲括号的符号主要用于teplates,而不是函数。