如何使用ACF返回WordPress REST API中的永久链接?

问题描述 投票:0回答:1

我正在使用ACF + ACF到REST API插件。

我有一个全局选项页面,其中包含一个链接到帖子的字段(这是一个ACF帖子关系字段)。

如果我导航到:/wp-json/acf/v3/options/options

响应如下:

{
  "acf": {
    "sticker_button": {
      "title": "Come & visit",
      "post_object": {
        "ID": 311,
        "post_author": "2",
        "post_date": "2019-04-30 18:39:50",
        "post_date_gmt": "2019-04-30 17:39:50",
        "post_content": "",
        "post_title": "test",
        "post_excerpt": "",
        "post_status": "publish",
        "comment_status": "closed",
        "ping_status": "closed",
        "post_password": "",
        "post_name": "test",
        "to_ping": "",
        "pinged": "",
        "post_modified": "2019-04-30 18:39:50",
        "post_modified_gmt": "2019-04-30 17:39:50",
        "post_content_filtered": "",
        "post_parent": 97,
        "guid": "https://example.dev.env.example.com/?page_id=311",
        "menu_order": 0,
        "post_type": "page",
        "post_mime_type": "",
        "comment_count": "0",
        "filter": "raw"
      }
    }
  }
}

因为我使用SPA来使用这些数据,所以我不想仅仅为了获得这个帖子永久链接/链接而发出另一个API请求。

有没有办法在post_object的上述帖子的响应中显示永久链接?

仅使用post_name手动构建链接是不够的,因为它可能是另一个的子页面。我需要在运行时实际永久链接,并且post_name不受页面层次结构的影响。

非常感谢您的帮助。

php json wordpress api advanced-custom-fields
1个回答
1
投票

看起来ACF to Rest插件有一个WP过滤器列表,您可以使用它来获取各种类型的WP REST Routes响应(请参阅here)。

我的建议是使用acf/rest_api/{type}/prepare_item过滤器,如果你想改变单个响应项的响应或acf/rest_api/{type}/get_fields过滤器对typeoption的所有对象 - 根据你问题中的REST路由。

这是使用acf/rest_api/{type}/get_fields(找到here)的示例代码:

function options_fields_filter( $data, $request ) {
   // here's where you'd mutate `$data` 

   return $data;
}

add_filter( 'acf/rest_api/option/get_fields', 'option_fields_filter', 10, 2 );

希望这可以帮助。如果您想要按照其他“类型”(例如特定帖子类型)使用更多指定的过滤器,请深入了解位于here的REST API控制器。

© www.soinside.com 2019 - 2024. All rights reserved.