Last active
April 11, 2024 09:05
-
-
Save igorbenic/d1071a205f9b43454e2e067de7cf6627 to your computer and use it in GitHub Desktop.
Headless WordPress: Displaying single articles | https://ibenic.com/headless-wordpress-displaying-articles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App() { | |
| const [loading, setLoading] = useState(false); | |
| const [articles, setArticles] = useState([]); | |
| const [notFoundSlugs, setNotFoundSlugs] = useState([]); // Adding not found slugs | |
| // ... other code | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Fetch the Article by Slug | |
| */ | |
| function fetchArticleBySlug( slug ) { | |
| const _url = url + 'wp/v2/posts?slug=' + slug; | |
| setLoading( true ); | |
| axios({ | |
| method: 'GET', | |
| url: _url | |
| }).then(function (response) { | |
| if ( response.status === 200 && response.data ) { | |
| const data = response.data; | |
| // If the article is found, add it to articles. | |
| if ( data.length ) { | |
| let _articles = [ ...articles, ...data ]; | |
| setArticles( _articles ); | |
| } else { | |
| // Set this slug to not found slugs. | |
| setNotFoundSlugs( slug ); | |
| } | |
| } | |
| setLoading(false); | |
| }) | |
| .catch(function (error) { | |
| console.error( error ); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Get the Article by Slug | |
| */ | |
| function GetArticleBySlug() { | |
| let { id } = useParams(); | |
| let article = {}; | |
| for( var i = 0; i < articles.length; i++ ) { | |
| if ( articles[i].slug === id ) { | |
| article = articles[i]; | |
| break; | |
| } | |
| } | |
| // If article was not found and we did not yet check this, try to fetch it. | |
| if ( notFoundSlugs.indexOf(id) < 0 && Object.keys(article).length === 0 ) { | |
| fetchArticleBySlug(id); | |
| } | |
| return (<Article article={ article } currentPage={ currentPage } />); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Not Found Page | |
| */ | |
| function PageNotFound() { | |
| return ( | |
| <article> | |
| <h1>Page Not Found</h1> | |
| <p>It seems the page you are looking for does not exist.</p> | |
| </article> | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Article Component | |
| */ | |
| function Article( props ) { | |
| const params = useParams(); | |
| const the_article = props.article; | |
| // No article? Show not found. | |
| if ( ! Object.keys(the_article).length ) { | |
| return <PageNotFound/>; | |
| } | |
| return ( | |
| <article id={the_article.ID}> | |
| <Link className='btn btn-primary' to={ "/page/" + props.currentPage }>Back</Link> | |
| <h1>{the_article.title.rendered}</h1> | |
| <div class="content" dangerouslySetInnerHTML={ { __html: the_article.content.rendered }}></div> | |
| </article> | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App { | |
| return ( | |
| <Router> | |
| <div className="App"> | |
| <div className="container"> | |
| { loading && <p>Loading</p> } | |
| <Switch> | |
| <Route path="/page/:page" render={routeProps => ( | |
| <ArticlePage | |
| currentPage={ currentPage } | |
| articles={ getArticlesForPage( currentPage, articles, articlesPages ) } | |
| totalPages={ totalPages } | |
| /> | |
| )}> | |
| </Route> | |
| <Route path="/:id"> | |
| <GetArticleBySlug /> | |
| </Route> | |
| <Route path="/" exact> | |
| <Articles | |
| currentPage={ currentPage } | |
| articles={ getArticlesForPage( currentPage, articles, articlesPages ) } | |
| totalPages={ totalPages } | |
| /> | |
| </Route> | |
| </Switch> | |
| </div> | |
| </div> | |
| </Router> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment