問題描述
我建立了 1 個自定義帖子型別與 2 個自定義分類法。安裝了 http://wordpress.org/extend/plugins/json-api/並試圖達到以下結果:http://example.com/api/get_recent_posts?dev=1&post_type=myposttype
它給我自定義帖子,但不給這些職位的自定義分類
“categories”:[],”tags”:[],
我如何用 json api 查詢自定義分類?
其實我正在嘗試用 jquery mobile + phonegap 建立一個簡單的 iphone 應用程式。也許你知道一個更好的方式然後 json api?
最佳解決辦法
我會這樣做的,我相信這裡的專家會有一個更好的方法,但下面是我可以趕上來。
首先在您的主題目錄 (或任何其他如果您喜歡) 建立您的控制器檔案與以下內容。對於這個例子,檔名是 korkmaz.php
更新 1:請替換以前的 korkmaz.php,因為擴充套件的內部審查是危險的許多不必要的功能透過 URI 暴露。現在我們有一個新的 JSON_API_Korkmaz_Controller 類,它不擴充套件任何其他類,我們已經刪除了 JSON_API_CustomPost 類。
更新 2:現在支援查詢自定義分類法,請參見底部的示例。包括新的模型類 JSON_API_Term 來表示任何型別的分類法的術語。
// most of the functions here are rewrite of json-api functions
class JSON_API_Korkmaz_Controller {
public function get_recent_posts() {
global $json_api;
$posts = $json_api->introspector->get_posts();
foreach ($posts as $jpost) {
$this->add_taxonomies( $jpost );
}
return $this->posts_result($posts);
}
protected function posts_result($posts) {
global $wp_query;
return array(
'count' => count($posts),
'count_total' => (int) $wp_query->found_posts,
'pages' => $wp_query->max_num_pages,
'posts' => $posts
);
}
protected function add_taxonomies( $post ) {
$taxonomies = get_object_taxonomies( $post->type );
foreach ($taxonomies as $tax) {
$post->$tax = array();
$terms = wp_get_object_terms( $post->id, $tax );
foreach ( $terms as $term ) {
$post->{$tax}[] = $term->name;
}
}
return true;
}
public function get_taxonomy_posts() {
global $json_api;
$taxonomy = $this->get_current_taxonomy();
if (!$taxonomy) {
$json_api->error("Not found.");
}
$term = $this->get_current_term( $taxonomy );
$posts = $json_api->introspector->get_posts(array(
'taxonomy' => $taxonomy,
'term' => $term->slug
));
foreach ($posts as $jpost) {
$this->add_taxonomies( $jpost );
}
return $this->posts_object_result($posts, $taxonomy, $term);
}
protected function get_current_taxonomy() {
global $json_api;
$taxonomy = $json_api->query->get('taxonomy');
if ( $taxonomy ) {
return $taxonomy;
} else {
$json_api->error("Include 'taxonomy' var in your request.");
}
return null;
}
protected function get_current_term( $taxonomy=null ) {
global $json_api;
extract($json_api->query->get(array('id', 'slug', 'term_id', 'term_slug')));
if ($id || $term_id) {
if (!$id) {
$id = $term_id;
}
return $this->get_term_by_id($id, $taxonomy);
} else if ($slug || $term_slug) {
if (!$slug) {
$slug = $term_slug;
}
return $this->get_term_by_slug($slug, $taxonomy);
} else {
$json_api->error("Include 'id' or 'slug' var for specifying term in your request.");
}
return null;
}
protected function get_term_by_id($term_id, $taxonomy) {
$term = get_term_by('id', $term_id, $taxonomy);
if ( !$term ) return null;
return new JSON_API_Term( $term );
}
protected function get_term_by_slug($term_slug, $taxonomy) {
$term = get_term_by('slug', $term_slug, $taxonomy);
if ( !$term ) return null;
return new JSON_API_Term( $term );
}
protected function posts_object_result($posts, $taxonomy, $term) {
global $wp_query;
return array(
'count' => count($posts),
'pages' => (int) $wp_query->max_num_pages,
'taxonomy' => $taxonomy,
'term' => $term,
'posts' => $posts
);
}
}
// Generic rewrite of JSON_API_Tag class to represent any term of any type of taxonomy in WP
class JSON_API_Term {
var $id; // Integer
var $slug; // String
var $title; // String
var $description; // String
function JSON_API_Term($term = null) {
if ($term) {
$this->import_wp_object($term);
}
}
function import_wp_object($term) {
$this->id = (int) $term->term_id;
$this->slug = $term->slug;
$this->title = $term->name;
$this->description = $term->description;
$this->post_count = (int) $term->count;
}
}
現在新增以下主題的 functions.php
// Add a custom controller
add_filter('json_api_controllers', 'add_my_controller');
function add_my_controller($controllers) {
$controllers[] = 'Korkmaz';
return $controllers;
}
// Register the source file for our controller
add_filter('json_api_korkmaz_controller_path', 'korkmaz_controller_path');
function korkmaz_controller_path($default_path) {
return get_stylesheet_directory() . '/korkmaz.php';
}
現在 goto Json API 設定頁面並啟用您的 korkmaz 控制器。
現在,您可以訪問以下 URL 的所有相關分類的自定義帖子型別:http://example.com/api/korkmaz/get_recent_posts/?post_type=myposttype
您可以使用以下示例查詢任何型別的分類 (包括自定義分類法):http://example.com/api/korkmaz/get_taxonomy_posts/?taxonomy=my_custom_taxonomy&slug=my_term_slug
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。