WordPress post值到新的类对象


WordPress post values into new class object

我正在尝试将WordPress post值放入一个新的类对象中,以便我可以返回到我的Ajax post

我现在得到的:

$(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 (Get WordPress post values)

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...

它没有返回带有键值的消息。有人知道为什么会这样吗?

预期结果

  • 键:getfirststimage值:Image
  • 键:getBlogDate值:日期
  • 键:getTitle值:Title
  • 键:getContent值:Content
  • 键:getAuthor值:Author

Tomasz Struczyński回答给出的输出:

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"}

更新添加的内容类型头集

首先,你应该考虑使用标准的Wordpress AJAX钩子,如下所述:https://codex.wordpress.org/AJAX_in_Plugins

关于你的方法:

在普通PHP中,您必须打印返回值。简单地从函数中返回它并不会导致它被发送回客户机。你必须以某种方式回应它。我建议使用JSON格式,因为它很容易编程。

为前端-添加dataType: '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及以下),因为没有卷曲大括号的符号主要用于模板,而不是函数。