WordPress - 如何为 json-api 输出所有类别下的所有今天帖子
我正在自定义 Wordpress 插件 JSON API。我想输出:
在一个大 JSON 树中显示每个类别今天发布的所有帖子。
IE:
Concept output:
Category 1
Post 1
Post 2
Category 2
No posts
Category 3
Post 1
Post 2
Post 3
我正在遵循有关抓取 按类别列出的最新帖子<的教程/a> 然而,这似乎表明我必须执行 2 个循环,一个循环获取所有类别,然后另一个循环获取该类别中的每个帖子。
我的自定义函数目前可以很好地获取类别,但我不确定如何使用当前的 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
我的输出是:
{
"status": "ok",
"count": 17,
"count_total": 4,
"pages": 1,
"posts": [
{
"id": 1,
"slug": "general",
"title": "General",
"description": "",
"parent": 0,
"post_count": 0
}
// etc
}
理想情况下,我想输出每个类别下今天的所有帖子,但我面临的问题是如何执行两个循环并将它们组合到 JSON API 插件中。
对此的任何帮助或指导将不胜感激。
谢谢。
编辑:
我找到了一个自定义的方法,这让我更近了一步。
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);
但是,这是不正确的,因为帖子不在 JSONPad 的屏幕截图中所示的类别内。
我知道我需要循环遍历外部类别,然后遍历其子类别以获得我想要的输出;然而问题是让 json-api 了解如何将数组组合成有效的 JSON 树。
I am customising the Wordpress plugin, JSON API. I want to output:
Display all published today's posts for each category in one big JSON tree.
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.
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.
// 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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我决定不通过从应用程序的 JSON API 获取类别,然后在需要时获取与我的类别相关的帖子来解决此问题。
这是一个漫长的过程,但现在就可以了。
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.
Its bit of a long-way round, but it will do for now.