【问题标题】:Wordpress ACF retrieve file data from REST APIWordpress ACF 从 REST API 检索文件数据
【发布时间】:2022-08-17 03:07:19
【问题描述】:

我是 WordPress 的新手,第一次使用 ACF 插件和 REST API。我创建了一个带有自定义字段的自定义帖子类型,其中一个是文件字段。我为帖子类型启用了 REST API 并且能够检索数据,但文件字段仅包含文件 ID。有什么方法可以获取文件 URL,而无需为该类型的每个帖子进行额外调用?

该自定义字段的返回值当前设置为文件数组,虽然我也尝试了文件 URL,但这似乎不会影响 REST API。

为测试帖子返回的格式化 JSON 示例:

[
   {
      \"id\":282,
      \"date\":\"2022-08-10T16:52:58\",
      \"date_gmt\":\"2022-08-10T16:52:58\",
      \"guid\":{
         \"rendered\":\"https:\\/\\/[domain]\\/?post_type=resourcelibrary_file&p=282\"
      },
      \"modified\":\"2022-08-10T16:52:58\",
      \"modified_gmt\":\"2022-08-10T16:52:58\",
      \"slug\":\"auto-draft\",
      \"status\":\"publish\",
      \"type\":\"resourcelibrary_file\",
      \"link\":\"https:\\/\\/[domain]\\/resourcelibrary_file\\/auto-draft\\/\",
      \"template\":\"\",
      \"meta\":[
         
      ],
      \"vl_bok_primary_cat\":[
         
      ],
      \"vl_cat\":[
         
      ],
      \"acf\":{
         \"title\":\"PR Test\",
         \"author\":\"Test Person\",
         \"year\":2022,
         \"keywords\":\"test keyword list\",
         \"category\":[
            21
         ],
         \"bok_cats\":[
            22,
            40
         ],
         \"file\":283
      },
      \"_links\":{
         \"self\":[
            {
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/resource_library_files\\/282\"
            }
         ],
         \"collection\":[
            {
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/resource_library_files\"
            }
         ],
         \"about\":[
            {
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/types\\/resourcelibrary_file\"
            }
         ],
         \"acf:term\":[
            {
               \"embeddable\":true,
               \"taxonomy\":\"vl_bok_primary_cat\",
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/vl_bok_primary_cat\\/40\"
            },
            {
               \"embeddable\":true,
               \"taxonomy\":\"vl_bok_primary_cat\",
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/vl_bok_primary_cat\\/22\"
            },
            {
               \"embeddable\":true,
               \"taxonomy\":\"vl_cat\",
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/vl_cat\\/21\"
            }
         ],
         \"wp:attachment\":[
            {
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/media?parent=282\"
            }
         ],
         \"wp:term\":[
            {
               \"taxonomy\":\"vl_bok_primary_cat\",
               \"embeddable\":true,
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/vl_bok_primary_cat?post=282\"
            },
            {
               \"taxonomy\":\"vl_cat\",
               \"embeddable\":true,
               \"href\":\"https:\\/\\/[domain]\\/wp-json\\/wp\\/v2\\/vl_cat?post=282\"
            }
         ],
         \"curies\":[
            {
               \"name\":\"wp\",
               \"href\":\"https:\\/\\/api.w.org\\/{rel}\",
               \"templated\":true
            }
         ]
      }
   }
]

谢谢!

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


    【解决方案1】:

    我能够在这篇文章中找到答案:https://support.advancedcustomfields.com/forums/topic/acf-rest-api-image-only-shows-attachment-id/

    将 ?acf_format=standard 添加到我的请求的末尾就可以了,并且除了包含 URL 的每个文件的 ID 之外,还添加了一个额外的信息数组。

    【讨论】:

      猜你喜欢
      • 2017-09-12
      • 2020-11-11
      • 2021-09-08
      • 2013-10-08
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多