Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何向wordpress json api核心控制器添加对象数组或额外字段?_Php_Wordpress_Object_Json Api - Fatal编程技术网

Php 如何向wordpress json api核心控制器添加对象数组或额外字段?

Php 如何向wordpress json api核心控制器添加对象数组或额外字段?,php,wordpress,object,json-api,Php,Wordpress,Object,Json Api,有没有办法向插件的核心控制器添加对象数组 我要修改的核心控制器是“get\u category\u index”,它返回这些对象 身份证 邮政编码 名字 鼻涕虫 我想做的是一个额外的对象“posts”,它列出了分配给该类别的帖子 当前,在jsonapi/controllers/core.php中,该特定控制器的代码如下所示 public function get_category_index() { global $json_api; $args = null; if (

有没有办法向插件的核心控制器添加对象数组

我要修改的核心控制器是“get\u category\u index”,它返回这些对象

  • 身份证
  • 邮政编码
  • 名字
  • 鼻涕虫
  • 我想做的是一个额外的对象“posts”,它列出了分配给该类别的帖子

    当前,在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返回中的帖子是空的,而我需要它来返回该类别中的帖子