【发布时间】:2020-04-12 14:26:28
【问题描述】:
在按类别过滤 Wordpress 帖子时,任何人都可以了解如何在 Gatsby 中对页面进行分页吗?
对于上下文,我的 gatsby-node 文件:
const path = require('path')
module.exports.createPages = async ({ graphql, actions }) => {
// import { paginate } from 'gatsby-awesome-pagination';
const { createPage } = actions
const blogPostTemplate = path.resolve('./src/templates/blog-post.js')
const blogCategoryFilter = path.resolve('./src/templates/blog-filter-category.js')
const blogArchiveFilter = path.resolve('./src/templates/blog-filter-archive.js')
const res = await graphql(`
query {
allWordpressPost {
edges {
node {
slug
date(formatString:"YYYY-MM")
}
}
}
allWordpressCategory {
edges {
node {
slug
}
}
}
}
`)
// UNPAGINATED
//Blog list - organized by category
res.data.allWordpressCategory.edges.forEach((edge) => {
createPage({
component: blogCategoryFilter,
path: `/blog/category/${edge.node.slug}`,
context: {
slug: edge.node.slug,
}
})
})
}
我用作模板的 blog-filter-category.js 文件:
import React from 'react'
import { graphql, Link } from 'gatsby'
import Layout from '../components/layout'
import BlogNav from '../components/blognav'
import blogStyles from '../components/modules/blog.module.css'
export const query = graphql`
query($slug: String!) {
allWordpressPost (filter: {categories: {elemMatch: {slug: { eq: $slug }}}}) {
edges {
node {
title
slug
content
date(formatString: "MMMM DD, YYYY")
}
}
}
}
`
export default ({ data }) => {
//const post = data.allWordpressPost.edges[0].node
return (
<Layout>
<div className={blogStyles.blog_container}>
<div className={blogStyles.blogContent_container}>
<ol>
{data.allWordpressPost.edges.map((edge) => {
return (
<div className={blogStyles.blogPost_container}>
<li className={blogStyles.blog_list}>
<h2><Link to={`/blog/${edge.node.slug}`} className={blogStyles.blog_title} dangerouslySetInnerHTML={{ __html: edge.node.title }}></Link></h2>
<p className={blogStyles.blog_date}>{edge.node.date}</p>
<p className={blogStyles.blog_content} dangerouslySetInnerHTML={{ __html: edge.node.content }} />
</li>
</div>
)
})}
</ol>
</div>
<BlogNav />
</div>
</Layout>
)
}
我尝试阅读一些相关插件的文档(gatsby-paginate、gatsby-awesome-paginate 等)和这篇文章 (https://www.gatsbycentral.com/pagination-in-gatsby),但这一切都让我有点不知所措。对于我在模板上生成并按时间顺序简单排序的博客文章似乎很有意义,但是当我开始按类别、存档月份等进行过滤时,我会感到困惑。
有什么建议吗?我可以使用上面的代码结构进行分页,还是必须重新考虑如何将它们组合在一起?
谢谢!
【问题讨论】:
-
如果它解决了您的问题,请确保接受我的回答,或者如果您需要在 cmets 中进行澄清,请告诉我。详情请见What should I do when someone answers my question?。
标签: javascript wordpress pagination graphql gatsby