如何在wordpress json api核心控制器中添加一个对象数组或一个额外的字段?

时间:2016-06-16 22:20:37

标签: php wordpress object json-api

有没有办法将对象数组添加到json api插件的核心控制器

我要修改的核心控制器是' get_category_index'并返回这些对象

  1. ID
  2. POST_COUNT
  3. 名称
  4. 蛞蝓
  5. 我想要做的是一个额外的对象,'帖子'列出分配给该类别的帖子

    目前在jsonapi / controllers / core.php中的代码看起来像这个特定的控制器看起来像那样

    public function get_category_index() {
        global $json_api;
        $args = null;
        if (!empty($json_api->query->parent)) {
          $args = array(
            'parent' => $json_api->query->parent
          );
        }
        $categories = $json_api->introspector->get_categories($args);
        return array(
      'count' => count($categories),
      'categories' => $categories
        );
      }
    

    我觉得这样的事情可以解决问题

    public function get_category_index() {
        global $json_api;
        $args = null;
        if (!empty($json_api->query->parent)) {
          $args = array(
            'parent' => $json_api->query->parent
          );
        }
        $categories = $json_api->introspector->get_categories($args);
        return array(
      'count' => count($categories),
      'categories' => $categories,
      'posts' => $posts,
        );
      }
    

    但我显然错过了一些东西,我怎么能添加那些帖子?

    我发现了category.php,看起来像这样

    class JSON_API_Category {
    
      var $id;          // Integer
      var $slug;        // String
      var $title;       // String
      var $description; // String
      var $parent;      // Integer
      var $post_count;  // Integer
      var $posts;  // array
    
      function JSON_API_Category($wp_category = null) {
        if ($wp_category) {
          $this->import_wp_object($wp_category);
        }
      }
    
      function import_wp_object($wp_category) {
        $this->id = (int) $wp_category->term_id;
        $this->slug = $wp_category->slug;
        $this->title = $wp_category->name;
        $this->description = $wp_category->description;
        $this->parent = (int) $wp_category->parent;
        $this->post_count = (int) $wp_category->count;
        $this->posts = $wp_category->posts;
      }
    
    }
    

    我添加了

    var $posts;  // array
    

    并称之为

    $this->posts = $wp_category->posts;
    

    然而很明显我在json回归中的帖子上得到了null,而我需要它来返回该类别中的帖子。

0 个答案:

没有答案