Add pagination
A key feature of any traditional, i.e. PHP-based, WordPress theme is the ability to paginate through the post listing. In this lesson we will look at implementing this feature in Frontity.
Click on the More posts link in the menu to navigate to /page/2 and examine frontity.state in the console. You will notice that the data for the page has both previous and next properties.
Frontity in the console
If you do the same for the home path, i.e. '/', you will notice that it doesn't have a previous property.
We can use these properties to add pagination to our <List> component. We will add a <PrevNextNav> component to style two buttons that enable us to page through the lists of posts.
Change list.js as follows. We're adding a <PrevNextNav> component which we use within the <List> component, and including buttons for the navigation.
Note that we need to pass actions into the <List> component.
1
// File: /packages/my-first-theme/src/components/list.js
2
​
3
import React from "react"
4
import { connect, styled } from "frontity"
5
import Link from "@frontity/components/link"
6
​
7
const List = ({ state, actions }) => {
8
const data = state.source.get(state.router.link)
9
​
10
return (
11
<Items>
12
{data.items.map((item) => {
13
const post = state.source[item.type][item.id]
14
return (
15
<Link key={item.id} link={post.link}>
16
{post.title.rendered}
17
</Link>
18
)
19
})}
20
<PrevNextNav>
21
{data.previous && (
22
<button
23
onClick={() => {
24
actions.router.set(data.previous)
25
}}
26
>
27
&#171; Prev
28
</button>
29
)}
30
{data.next && (
31
<button
32
onClick={() => {
33
actions.router.set(data.next)
34
}}
35
>
36
Next &#187;
37
</button>
38
)}
39
</PrevNextNav>
40
</Items>
41
)
42
}
43
​
44
export default connect(List)
45
​
46
const Items = styled.div`
47
& > a {
48
display: block;
49
margin: 6px 0;
50
font-size: 1.2em;
51
color: steelblue;
52
text-decoration: none;
53
}
54
`
55
const PrevNextNav = styled.div`
56
padding-top: 1.5em;
57
​
58
& > button {
59
background: #eee;
60
text-decoration: none;
61
padding: 0.5em 1em;
62
color: #888;
63
border: 1px solid #aaa;
64
font-size: 0.8em;
65
margin-right: 2em;
66
}
67
& > button:hover {
68
cursor: pointer;
69
}
70
`
Copied!
We've included conditional checks so that the 'Previous' button doesn't show on the first page and the 'Next' button doesn't show on the last page, as they're not needed in those locations. To do this we've used "short-circuit" evaluation.
Now that we have pagination we no longer need the 'More posts' option in the menu. Let's remove it.
1
// File: /packages/my-first-theme/src/components/index.js
2
​
3
// ...
4
const Root = ({ state, actions }) => {
5
​
6
// ...
7
<Header isPostType={data.isPostType} isPage={data.isPage}>
8
<HeaderContent>
9
// ...
10
<Menu>
11
<Link link="/">Home</Link>
12
<Link link="/about-us">About Us</Link>
13
</Menu>
14
</HeaderContent>
15
</Header>
16
<Main>
17
// ...
18
</Main>
19
// ...
20
};
Copied!
Check you're on the right track by comparing your changes with the ones in this commit.
Last modified 5mo ago
Copy link