简体   繁体   English

React.js 中的 AFC 中继器和 wp-rest api

[英]AFC Repeater and wp-rest api in React.js

Need to render ACF repeater in react.需要在反应中渲染 ACF 中继器。 I am able to display ACF text Fields but not repeater fields.我能够显示 ACF 文本字段,但不能显示转发器字段。 Need to find out if anyone has an example of how to map through a repeater field.需要找出是否有人有如何通过中继器字段 map 的示例。

Repeater field group Is called Skills.中继器字段组称为技能。

Im also new in this stuff, but I will try to help you.我也是这方面的新手,但我会尽力帮助你。

So, the first thing that you need is to download and install ACF to REST API plugin so you can use ACF with Wordpress API.因此,您需要做的第一件事是下载 ACF 并将其安装到 REST API 插件,这样您就可以将 ACF 与 Wordpress16DE427CED014CA3 一起使用。 I assume, that you already have it, because as you said before - you can display text fields.我假设您已经拥有它,因为正如您之前所说 - 您可以显示文本字段。

Once you can send data through Wordpress API, you need to preview of JSON sent by Wordpress (in this case), so you can display necessary data.一旦您可以通过 Wordpress API 发送数据,您需要预览 ZDCEC9B13C1DE515016A3BC0F 发送的 JSON 的数据(在这种情况下,您可以显示必要的数据)。 Mine is called React Developer Tools and I installed it as Chrome extension.我的称为 React Developer Tools,我将其安装为 Chrome 扩展。

Link to Chrome store 链接到 Chrome 商店

It should look like this:它应该如下所示:

在此处输入图像描述

As you can see, my component is called Home.js, yours may be called differently.如您所见,我的组件称为 Home.js,您的组件可能会有所不同。 Chose component that is fetching all the data that you need.选择正在获取您需要的所有数据的组件。

Now, you just need to use your repeater.现在,您只需要使用中继器即可。 It would be much easier if you showed us your code.如果您向我们展示您的代码会容易得多。 I don't really know what kind of data you are calling through api, so I guess these are pages.我真的不知道你通过 api 调用什么样的数据,所以我猜这些是页面。

{ pages[0].acf.technologie_lista.map ( (field, index) => (
     <div key={index} className="single-field">
        { field.nazwa_technologii }
     </div>
) ) } 

Let's break it down.让我们分解一下。

1 - My project contains two pages. 1 - 我的项目包含两个页面。 I have chosen the first one, because only this one has needed ACF fields.我选择了第一个,因为只有这个需要 ACF 字段。 technologie_lista is acf field name. technologie_lista是 acf 字段名称。

2 - You need to use map function to list all posts. 2 - 您需要使用 map function 列出所有帖子。 You need to assign key to each element.您需要为每个元素分配键。

  1. nazwa_technologii is just a repeater sub field name. nazwa_technologii只是一个中继器子字段名称。

And that's all.就这样。 I might make some rookie mistakes, but it work's for me.我可能会犯一些新手错误,但它对我有用。 I hope that i helped.我希望我有所帮助。 Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM