WordPress json自定义分类问题

时间:2011-05-14 作者:Ünsal Korkmaz

我创建了1个自定义帖子类型和2个自定义分类法。已安装http://wordpress.org/extend/plugins/json-api/ 并试图通过以下方式获得结果:http://example.com/api/get_recent_posts?dev=1&post_type=myposttype

它给了我自定义的帖子,但没有提供这些帖子的自定义分类

“类别”:[],“标记”:[],

如何使用json api查询自定义分类?

实际上,我正在尝试用jquery mobile+phonegap创建一个简单的iphone应用程序。也许你知道比json api更好的方法?

3 个回复
最合适的回答,由SO网友:Hameedullah Khan 整理而成

我会这样做,我相信这里的专家会有更好的方法,但下面是我可以很快想出的。

首先在您的主题目录(或任何其他目录,如果您喜欢)中使用以下内容创建控制器文件。对于本例,文件名为korkmaz。php

UPDATE 1: 请更换以前的korkmaz。php因为扩展内省程序很危险,许多不需要的函数都是通过URI公开的。现在我们有了new JSON_API_Korkmaz_Controller class 哪一个does not extend 任何其他班级,我们有removed the JSON_API_CustomPost class.

更新2:现在支持查询自定义分类法,请参见底部的示例。包括新的模型类JSON\\U API\\U 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;
  }

}
现在将以下内容添加到主题的函数中。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\';
}
现在转到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

SO网友:edzillion

我刚刚测试过这个(我正在为一个项目做完全相同的事情),它对我来说很好。

向自定义帖子类型添加了标记,运行了以下操作:

http://example.com/?json=get_recent_posts&post_type=custompost
json文件如下所示:

{
    "status":"ok",
    "count":10,
    "count_total":11,
    "pages":2,
    "posts":[
    {
        "id":80,
        "type":"custompost",
        "slug":"custompost-12",
        "url":"http:\\/\\/example.com\\/?custompost=custompost-12",
        "status":"publish",
        "title":"custompost 12",
        "title_plain":"custompost 12",
        "content":"<p>12<\\/p>\\n",
        "excerpt":"12",
        "date":"2011-05-26 12:32:59",
        "modified":"2011-05-26 13:54:03",
        "categories":[],
        "tags":[
            {"id":4,"slug":"tag1","title":"tag1","description":"","post_count":10},
            {"id":6,"slug":"tag2","title":"tag2","description":"","post_count":6},
            etc...
是否确实已将自定义分类添加到自定义帖子类型?

add_action( \'init\', \'create_my_post_types\' );   
function create_my_post_types() {
    register_post_type( \'custompost\',
        array(
            ...
            \'taxonomies\' => array( \'category\', \'post_tag\' ),
            ...etc
如果您想做一些更复杂的事情,比如定义自定义分类法的层次结构,this link might help you.

结束