从WP-API获取类别名称而不是ID

时间:2017-12-06 作者:moeses

我正在尝试获取类别name 而不是ID 来自我的自定义帖子类型的WP REST API。一些关于端点修改的文章给了我一些关于如何解决它的想法,但不幸的是,我没有让它发挥作用。这是我的代码(*删除了一些行中不相关的代码):

CUSTOM POST TYPE

<?php
  add_action( \'init\', \'portfolio_projects\' );
        function portfolio_projects() {
          $labels = array([...]);

          $args = array(
             [...]
              \'show_in_rest\'          => true,
              \'rest_base\'             => \'projects\',
              \'rest_controller_class\' => \'Category_Data\',
              \'supports\'              => array( \'title\', \'thumbnail\', \'editor\'),
              \'taxonomies\'            => array(\'post_tag\', \'category\')
          );

          register_post_type( \'project\', $args );
      }

CONTROLLER CLASS

<?php
/**
 * Category data
 */
class Category_Data extends WP_REST_Posts_Controller
{

    public function init()
    {
        add_action(\'rest_api_init\', array(
            $this,
            \'add_category_data\'
        ));
    }

    /**
     * Add the category data
     */
    public function add_category_data()
    {
        register_rest_field(\'project\', \'category_data\', [\'get_callback\' => array(
            $this,
            \'get_all_category_data\'
        ) , ]);
    }

    /**
     * Get all the category data
     *
     * @param $object
     * @param $field_name
     * @param $request
     *
     * @return array
     */
    public function get_all_category_data($object, $field_name, $request)
    {
        return get_the_category($object[\'id\']);
    }
}
我很想听听你对此的想法和想法。谢谢

2 个回复
SO网友:kierzniak

此代码将添加categories_names wp rest api响应字段:

function wpse_287931_register_categories_names_field() {

    register_rest_field( \'project\',
        \'categories_names\',
        array(
            \'get_callback\'    => \'wpse_287931_get_categories_names\',
            \'update_callback\' => null,
            \'schema\'          => null,
        )
    );
}

add_action( \'rest_api_init\', \'wpse_287931_register_categories_names_field\' );

function wpse_287931_get_categories_names( $object, $field_name, $request ) {

    $formatted_categories = array();

    $categories = get_the_category( $object[\'id\'] );

    foreach ($categories as $category) {
        $formatted_categories[] = $category->name;
    }

    return $formatted_categories;
}

SO网友:Adrián Benavente

同上,但将“project”替换为array(\'post\') 应该有用。

function wpse_287931_register_categories_names_field()
{
    register_rest_field(
        array(\'post\'),
        \'categories_names\',
        array(
            \'get_callback\'    => \'wpse_287931_get_categories_names\',
            \'update_callback\' => null,
            \'schema\'          => null,
        )
    );
}

add_action(\'rest_api_init\', \'wpse_287931_register_categories_names_field\');

function wpse_287931_get_categories_names($object, $field_name, $request)
{
    $formatted_categories = array();

    $categories = get_the_category($object[\'id\']);

    foreach ($categories as $category) {
        $formatted_categories[] = $category->name;
    }

    return $formatted_categories;
}

结束