简体   繁体   English

WordPress-如何输出json-api所有类别下的所有今日帖子

[英]Wordpress - How to output all todays posts under all categories for json-api

I am customising the Wordpress plugin, JSON API . 我正在自定义Wordpress插件JSON API I want to output: 我想输出:

Display all published today's posts for each category in one big JSON tree. 在一个大的JSON树中显示每个类别的所有今天发布的所有帖子。

IE: IE浏览器:

Concept output:

Category 1  
Post 1  
Post 2

Category 2  
No posts

Category 3  
Post 1  
Post 2  
Post 3

I am following the tutorial about grabbing the latest posts by category however this seems to indicate I have to do 2 loops, one to grab all the categories and then another to grab each post within that category. 我正在按照按类别抓取最新帖子的教程,但这似乎表明我必须做2个循环,一个抓取所有类别,然后另一个抓取该类别中的每个帖子。

My custom function currently grabs the categories fine, but I am unsure of how to combine the two loops into a JSON tree using the current JSON API plugin. 我的自定义函数当前可以很好地捕获类别,但是我不确定如何使用当前的JSON API插件将两个循环合并到JSON树中。

// This is in the introspector.php file
public function get_todays_posts($today)
{
    global $wpdb;

    /* // Show posts for category 5 - this works, but it doesn't show the category name
    $q = query_posts('cat=5&year='.$today["year"].'&monthnum='.$today["month"].'&day=' .$today["day"].'&showposts=-1&orderby=post_date&order=desc');
*/

    // output for this is below
    $sql = "SELECT * FROM gc_terms AS wterms INNER JOIN gc_term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category'";

    $data = $wpdb->get_results(($sql));

    $results = array();
    foreach ($data as $row)

    {
    $results[] = new JSON_API_Category($row);
    }

    return($results);
} // end function

My output is: 我的输出是:

{
  "status": "ok",
  "count": 17,
  "count_total": 4,
  "pages": 1,
  "posts": [
    {
      "id": 1,
      "slug": "general",
      "title": "General",
      "description": "",
      "parent": 0,
      "post_count": 0
    }
 // etc
}

Ideally I would like to output all of todays posts under each category, but the problem that I am facing is how to do two loops and combine them into the JSON API plugin. 理想情况下,我想输出每个类别下的所有今日帖子,但是我面临的问题是如何执行两个循环并将它们组合到JSON API插件中。

Any help or guidance on this would be most appreciated. 任何帮助或指导,将不胜感激。

Thanks. 谢谢。

EDIT: 编辑:

I have found a method which I have customized which gets me a step closer 我找到了一种自定义的方法,使我更近了一步

global $json_api;

$category = $json_api->introspector->get_category_by_id(5);
if (!$category) {
  $json_api->error("Not found.");
}

$posts = $json_api->introspector->get_posts(array(
    'cat' => $category->id,
    'post_status' => 'publish',
    'year' => 2011,
    'monthnum' => 10,
    'day' => 01,
    'orderby' => 'post_date',
    'order' => 'ASC'
));

return $this->posts_object_result($posts, $category);

However, this is in-correct because posts are not within the category as seen in this screenshot from JSONPad. 但是,这是不正确的,因为帖子不在JSONPad的此屏幕截图中所示的类别之内。

jsonpad屏幕截图

I understand that I need to loop through the outer categories and then its children to get the output I want; 我知道我需要遍历外部类别,然后遍历其子类别以获得所需的输出; however the problem is making json-api understand how to combine arrays into a valid JSON tree. 但是问题是使json-api了解如何将数组组合成有效的JSON树。

I have decided not to resolve this problem by getting the categories from the JSON API from my app and then getting the posts relevant to my category as-and-when I need them. 我决定不解决此问题,方法是从我的应用程序中从JSON API中获取类别,然后在需要时随时获取与我的类别相关的帖子。

Its bit of a long-way round, but it will do for now. 这是一个漫长的回合,但现在就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM