【问题标题】:Show Category icon in REST API wordpress在 REST API wordpress 中显示类别图标
【发布时间】:2021-01-04 14:28:30
【问题描述】:

我使用ACF将类别图标添加到类别中作为ACF,并且已经添加成功, 现在我想在 REST API 输出中显示这个字段: 我使用了这段代码,但它不起作用。 get_field 和 get_field_object 只是在 REST API 输出中返回 null(见附图) 这是实时输出: http://134.209.149.245/index.php?rest_route=/wp/v2/categories

function wpse_2879312_register_category_icon_field()
{
    register_rest_field(
        array('category'),
        'category_icon',
        array(
            'get_callback'    => 'wpse_2879312_get_category_icon',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

add_action('rest_api_init', 'wpse_2879312_register_category_icon_field');

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

    $category_icon = get_field_object('category_icon' , get_the_ID());
    return $category_icon ['value'];
}

【问题讨论】:

    标签: wordpress advanced-custom-fields wordpress-rest-api


    【解决方案1】:

    有一个很好的插件可以公开你的 ACF 字段 https://wordpress.org/plugins/acf-to-rest-api/ 将为您节省大量时间并且效果很好。

    【讨论】:

      猜你喜欢
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多