我正在使用 react 构建我的第一个完整的生产网站,但我遇到了 WP REST API 的问题。我有一个博客页面,其中列出了我在 WordPress 中的帖子,没问题。唯一的问题是,我映射了帖子数组,作者是一个数字(作者 ID)。
在这种情况下如何获取该用户 ID?
import React, { Component } from "react"
...
class Blog extends Component {
static async getInitialProps() {
const postsRes = await fetch(`${Config.apiUrl}/wp-json/wp/v2/posts?_embed`)
const post = await postsRes.json()
return(post)
}
render() {
const posts = this.props.posts.map((post,index) => {
return(
<li key={index}>
{post.better_featured_image != null ? <Feat img=
{post.better_featured_image.source_url}/> : ""}
<Link
as={`/blog/${post.slug}`}
href={`/blog?slug=${post.slug}&apiRoute=post`}
>
<a>{post.title.rendered}</a>
<p>{post.author}</p> //Returns ID I need name
</Link>
</li>
)
})
}
}
我希望能够将作者姓名放在我上面的评论中。我已经尝试过构建功能,但我无法弄清楚。我试图弄清楚是否应该调用一个函数,该函数可以从 post.author 传回该 ID,然后将其附加到 /wp-json/wp/v2/users/ Insert Here
任何提示或建议!
(对不起,如果我有轻微的语法错误,我使用样式化的组件,我只用标准的 jsx 元素手动输入)