#New to Astro , blog post with previous and next link

1 messages · Page 1 of 1 (latest)

past pendant
#

Hello, I am new to astro.

I am trying to add **next **and **previous **under each blog post. I tried reading the https://docs.astro.build/en/core-concepts/routing/ but could not understand the concepts. I am not a developer. I tried looking at different themes, looks like non has that concept. At most they have the concept of pagination and related posts kind of thing but that is not what I am looking.

Sorry for the newbie question in advance.

Astro Documentation

An intro to routing with Astro.

merry patio
#

There is a simple method that you can manually add the URLs of prev post and next post to the frontmatter of each of your blog articles, and then get these two URLs externally and add them to the corresponding buttons for routing.

merry patio
past pendant
#

Thanks, @merry patio for the info; let me read and try the content collections approach and see if this is what I am looking for.

past pendant
#

OK ; so I converted my repo to a content collection as directed by the documenttaion. Now how I get the previous and next blog post entry .

shut sonnet
#

you could just get the index of the current entry in the collection array and then display data from the previous/next entry where you want it

#

something like this:

const entries = await getCollection('posts')
const thisPostIndex = entries.indexOf(entries.find((el) => el.slug === Astro.props.slug))

const nextPost = entries[thisPostIndex + 1]
const previousPost = entries[thisPostIndex - 1]
#

but maybe there is an easier way or a plugin? not sure.