File tree 4 files changed +103
-9
lines changed
4 files changed +103
-9
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import Link from 'next/link'
3
3
export default ( ) => (
4
4
< div id = "navigation" >
5
5
< Link href = "/" > < a href = "/" > Home</ a > </ Link >
6
- < Link href = "/posts" > < a href = "/posts" > Posts </ a > </ Link >
6
+ < Link href = "/posts" > < a href = "/posts" > Blog </ a > </ Link >
7
7
< style jsx > { `
8
8
#navigation {
9
9
overflow: hidden;
Original file line number Diff line number Diff line change
1
+ import Head from 'next/head'
1
2
import Navigation from '../components/Navigation'
2
3
import { Fragment } from 'react'
3
4
4
5
export default ( ) => (
5
6
< Fragment >
6
7
< Navigation />
8
+ < Head >
9
+ < title > Home Page</ title >
10
+ < meta name = "description" content = "Headless WP Home Page" />
11
+ < meta charSet = "utf-8" />
12
+ < meta name = "viewport" content = "initial-scale=1.0, width=device-width" />
13
+ </ Head >
7
14
< h1 > Your new server-side rendered React.js app!</ h1 >
8
15
</ Fragment >
9
16
)
Original file line number Diff line number Diff line change 1
1
import Navigation from '../components/Navigation'
2
- import { Fragment } from 'react'
3
-
4
- export default ( ) => (
5
- < Fragment >
6
- < Navigation />
7
- < h1 > Our Posts Page!</ h1 >
8
- </ Fragment >
9
- )
2
+ import React , { Component , Fragment } from 'react'
3
+ import axios from 'axios'
4
+ import Link from 'next/link'
5
+ import Head from 'next/head'
6
+
7
+ export default class extends Component {
8
+
9
+ // Resolve promise and set initial props.
10
+ static async getInitialProps ( ) {
11
+
12
+ const siteurl = 'pmcsk.test' ;
13
+
14
+ // Make request for posts.
15
+ const response = await axios . get ( 'http://' + siteurl + '/wp-json/wp/v2/posts' ) ;
16
+
17
+ // Return response to posts object in props.
18
+ return {
19
+ posts : response . data
20
+ }
21
+ }
22
+
23
+ render ( ) {
24
+ return (
25
+ < Fragment >
26
+ < Navigation />
27
+ < Head >
28
+ < title > Blog Page</ title >
29
+ < meta name = "description" content = "Headless WP Blog Page" />
30
+ < meta charSet = "utf-8" />
31
+ < meta name = "viewport" content = "initial-scale=1.0, width=device-width" />
32
+ </ Head >
33
+ < h1 > Our Posts Page!</ h1 >
34
+ < ul >
35
+ {
36
+ this . props . posts . map ( post => {
37
+ return (
38
+ < li key = { post . id } >
39
+ < Link href = { `/posts/${ post . slug } ` } >
40
+ < a href = { `/posts/${ post . slug } ` } dangerouslySetInnerHTML = { {
41
+ __html : post . title . rendered
42
+ } } />
43
+ </ Link >
44
+ </ li >
45
+ )
46
+ } )
47
+ }
48
+ </ ul >
49
+ </ Fragment >
50
+ )
51
+ }
52
+ }
Original file line number Diff line number Diff line change
1
+ import Navigation from '../components/Navigation'
2
+ import React , { Component } from 'react'
3
+ import axios from 'axios' ;
4
+ import { Fragment } from 'react'
5
+ import Head from 'next/head'
6
+
7
+ export default class extends Component {
8
+
9
+ // Resolve promise and set initial props.
10
+ static async getInitialProps ( context ) {
11
+
12
+ const siteurl = 'pmcsk.test' ;
13
+
14
+ const slug = context . query . slug
15
+
16
+ // Make request for posts.
17
+ const response = await axios . get ( 'http://' + siteurl + `/wp-json/wp/v2/posts?slug=${ slug } ` )
18
+
19
+ // Return our only item in array from response to posts object in props.
20
+ return {
21
+ post : response . data [ 0 ]
22
+ }
23
+ }
24
+
25
+ render ( ) {
26
+ return (
27
+ < Fragment >
28
+ < Navigation />
29
+ < Head >
30
+ < title dangerouslySetInnerHTML = { { __html : this . props . post . title . rendered } } />
31
+ < meta name = "description" content = { this . props . post . title . rendered } />
32
+ < meta charSet = "utf-8" />
33
+ < meta name = "viewport" content = "initial-scale=1.0, width=device-width" />
34
+ </ Head >
35
+ < h1 dangerouslySetInnerHTML = { { __html : this . props . post . title . rendered } } />
36
+ < article
37
+ className = "entry-content"
38
+ dangerouslySetInnerHTML = { {
39
+ __html : this . props . post . content . rendered
40
+ } } />
41
+ </ Fragment >
42
+ )
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments