diff --git a/gatsby-node.js b/gatsby-node.js index 81ece48..161bd3b 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -114,6 +114,15 @@ exports.createPages = ({ actions, graphql }) => { } } } + allWordpressPost { + edges { + node { + categories { + slug + } + } + } + } } `) }) @@ -124,11 +133,23 @@ exports.createPages = ({ actions, graphql }) => { } const categoriesTemplate = path.resolve(`./src/templates/category.js`) - // Create a Gatsby page for each WordPress Category _.each(result.data.allWordpressCategory.edges, ({ node: cat }) => { - createPage({ - path: `/categories/${cat.slug}/`, + // Filter for posts attached to category + const catPosts = result.data.allWordpressPost.edges.filter( + ({ node }) => + node.categories && + node.categories.filter(postCat => postCat.slug === cat.slug) + ) + // Create a paginated category archive, e.g., /category/, /category//page/2, + paginate({ + createPage, + items: catPosts, + itemsPerPage: 10, + pathPrefix: ({ pageNumber }) => + pageNumber === 0 + ? `/category/${cat.slug}` + : `/category/${cat.slug}/page`, component: categoriesTemplate, context: { name: cat.name, @@ -149,6 +170,15 @@ exports.createPages = ({ actions, graphql }) => { } } } + allWordpressPost { + edges { + node { + tags { + slug + } + } + } + } } `) }) @@ -163,8 +193,18 @@ exports.createPages = ({ actions, graphql }) => { // Create a Gatsby page for each WordPress tag _.each(result.data.allWordpressTag.edges, ({ node: tag }) => { - createPage({ - path: `/tags/${tag.slug}/`, + // Filter for posts attached to tag + const tagPosts = result.data.allWordpressPost.edges.filter( + ({ node }) => + node.tags && node.tags.filter(postTag => postTag.slug === tag.slug) + ) + // Create a paginated tag archive, e.g., /tag/, /tag//page/2, + paginate({ + createPage, + items: tagPosts, + itemsPerPage: 10, + pathPrefix: ({ pageNumber }) => + pageNumber === 0 ? `/tag/${tag.slug}` : `/tag/${tag.slug}/page`, component: tagsTemplate, context: { name: tag.name, @@ -180,10 +220,20 @@ exports.createPages = ({ actions, graphql }) => { edges { node { id + name slug } } } + allWordpressPost { + edges { + node { + author { + slug + } + } + } + } } `) }) @@ -196,11 +246,23 @@ exports.createPages = ({ actions, graphql }) => { const authorTemplate = path.resolve(`./src/templates/author.js`) _.each(result.data.allWordpressWpUsers.edges, ({ node: author }) => { - createPage({ - path: `/author/${author.slug}`, + // Filter for posts attached to author + const authorPosts = result.data.allWordpressPost.edges.filter( + ({ node }) => node.author.slug === author.slug + ) + // Create a paginated author archive, e.g., /author/, /author//page/2, + paginate({ + createPage, + items: authorPosts, + itemsPerPage: 10, + pathPrefix: ({ pageNumber }) => + pageNumber === 0 + ? `/author/${author.slug}` + : `/author/${author.slug}/page`, component: authorTemplate, context: { - id: author.id, + name: author.name, + slug: author.slug, }, }) }) diff --git a/src/components/Pagination.js b/src/components/Pagination.js index 89c3a98..63870fe 100644 --- a/src/components/Pagination.js +++ b/src/components/Pagination.js @@ -1,8 +1,14 @@ import React from 'react' import { Link } from 'gatsby' -const Pagination = ({ pageContext, pathPrefix }) => { - const { previousPagePath, nextPagePath } = pageContext +const Pagination = ({ pageContext, endSize = 1, midSize = 1 }) => { + const { + previousPagePath, + nextPagePath, + numberOfPages, + humanPageNumber, + } = pageContext + let dots = false return (