Next.js Discord

Discord Forum

Dynamic router for categories (posts)

Unanswered
Anthony Fubon posted this in #help-forum
Open in Discord
Hey, everybody. Can you please tell me how to make dynamic switching of posts when selecting a category? It doesn't work for me, all categories are displayed.

An example of what I have.
https://next-sanity-blog.thefubon.dev/pages

https://github.com/thefubon/next-sanity-blog/tree/main/app/(frontend)/pages

If you select a category, all posts are loaded. But I need to display only the selected category.
I'm using Sanity.io

If it's not too much trouble, please help

6 Replies

Oriental chestnut gall wasp
could you use something like Array.filter(post => post.category === "my category" ?
I think we need more information about how your categories are defined
@Oriental chestnut gall wasp

I added a link to github and there is an example of what I did and this one doesn't work. I don't know how to do it 😦

I wanted to do the same as it is done on NextJS site

https://nextjs.org/showcase

There's a category toggle and a button to download more

I don't understand how to make categories for Next + Sanity CMS
I tried this solution, as shown in the screenshot. If I specify a specific category it is displayed. Now I can't figure out how to correctly specify the value slug.current instead of 'device' so that when I click on a category on the site, it opens exactly as specified in the browser bar.
Oriental chestnut gall wasp
It looks to me like what you could do is add a useLocation hook to access the current pathname, then parse it out and use that where you currently have "device" hard-coded in your query.
That should get you going in the right direction at least 🙂 but feel free to ping me if you want code snippets