Since we can’t determine the post type of pages vs posts by URI (due to how the permalinks are structured for those) we need to refactor to query all pages and post types like this.
This greatly simplifies how we’re handling routing since it will eliminate the need for subdirectories under the /frontend/pages path. With that in place we can simply dynamically render the correct single-item template (post/page/product/promotion/location, etc.) based on the post type of the node that’s returned.
Real Data
We can pull JSON data from Sitefinity sites like this.
Connect to Dev Database
You can connect to the demo site's database to work with large data sets to save time. Just make the backend localhost url the same as the dev site url in the constants.ts file.
You can switch to the dev db to work with some actual data on the frontend. Just be sure not to alter it in any way. For alterations, you can export the data from WPAllImport
By default, posts come from the JSON API with only the ID of a featured image. But, there are embedded fields that can be utilized to get the image data.
// WHEN THE CURRENT CATEGORY CHANGES, TO UPDATE THE CATEGORY ARRAYconstselectCategoryArray=useSelect( (select) => {returnselect("core").getEntityRecords("postType", currentCategory, { _embed:true, }); }, [currentCategory]) asany[];
Here we are getting an array of posts from the WP database using getEntityRecords. The third argument allows us to add options. When we set _embed to true, we get the extended data that we need to display images on the frontend.
// THIS LOGIC MAKES IT POSSIBLE TO SHOW SWIPABLE CARDS WITH OR WITHOUT IMAGESlet cardImage;if (post?._embedded["wp:featuredmedia"]) { cardImage = { sourceUrl:post?._embedded["wp:featuredmedia"][0]?.source_url, title:post?.title?.rendered ??"", altText:post?._embedded["wp:featuredmedia"][0]?.alt_text ??post?.title?.rendered };cardProps.showPlaceHolderImage =true;}
This is what the image data looks like in a loop over that array of posts when called from the frontend component.
Manually Revalidate Cache
Paste this curl request into Insomnia to manually revalidate cache.