Hey there, I’m trying to use Ghost as a backend and Nextjs as a frontend. I have got the post to display, but I can’t display the author name. Does anyone know how to do that?
The most common gotcha when fetching posts from the Content API is not using the include parameter to request related data such as tags and authors. By default, the response for a post will not include this data.
What does your API call look like?
this is what it looks like
import GhostContentAPI from "@tryghost/content-api";
// Create API instance with site credentials
const api = new GhostContentAPI({
url: 'Redacted',
key: 'Redacted
version: "v5.0"
});
export async function getPosts() {
return await api.posts
.browse({
include: "tag,authors",
limit: "all"
})
.catch(err => {
console.error(err);
});
}
export async function getSinglePost(postSlug) {
return await api.posts
.read({
slug: postSlug
})
.catch(err => {
console.error(err);
});
}
On your getPosts
function, you should have tags
plural.
You’ll also need to have the include: "tags,authors"
on your getSinglePost
function. Otherwise, author data won’t be returned.
With those in place you should receive tag and author data from the API call.
Like this?
export async function getPosts() {
return await api.posts
.browse({
include: "tags,authors",
limit: "all"
})
.catch(err => {
console.error(err);
});
}
export async function getSinglePost(postSlug) {
return await api.posts
.read({
include: "tags,authors",
slug: postSlug
})
.catch(err => {
console.error(err);
});
}