drb88830 2016-06-16 22:20
浏览 48

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

Is there a way to add an array of objects to a core controller of the json apiplugin

the core controller i want to modify is 'get_category_index' and it returns these objects

  1. id
  2. post_count
  3. name
  4. slug

what i want to do is an extra object, 'posts' which lists the posts that are assigned to that category

the code currently looks like this in jsonapi/controllers/core.php for that specific controller looks like that

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
    );
  }

and i thought something like this would do the trick

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,
    );
  }

but i'm obviously im missing something, how can i add that array of posts?

i found the category.php which looks like this

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;
  }

}

where i added the

var $posts;  // array

and called it

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

however obviously i'm getting null on posts in my json return, whereas i need it to return the posts in that category.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度