Hi,
trying to consolidate a few discussions around this topic with a specific scenario
letās say I want an Events page which has both intro copy (the content field) and a paged list of events.
thereās usually 2 ways to do this in Wordpress (assuming a CPT āeventā and some custom routing)
a) an /events archive template that pulls in content from a separate Events page (usually with the slug /events in the WP admin, so requires some routing overrides)
OR
b) an Events page /events that runs a custom loop to get all the CPT items
I think a) is usually easier to paginate since WP sees it is an archive by default.
//
I am currently creating the CPT with CPT UI plugin. Iāve got the REST option set to true and the archive slug set to event-archive (although actually it would probably just want to be events)
In frontity, I want to be able to
- display this Events page with content + list when I visit
/events - have access to the list of
eventsposts for rendering on other pages if necessary
the problems here are
-
if I set the archive to
"/events"in settings for my post type then I canāt fetch data from the Events page which also has the same post slug"/events" -
if I set the archive setting to
/event-archive(matching whatās in the WP CPT admin), then it creates a browser route at/event-archivewhich I donāt want but at least allows me to call eg this in myevents.jsfile
const event_data = state.source.get(state.router.link) // ('/event-archive')
const events = event_data.items
const post_data = state.source.get("/events") // my Events page
const post = state.source[post_data.type][post_data.id]
however as noted, I actually want the user-facing URL for this page to be /events which ideally Iād set as the archive slug
is the only option for my /events URL to create a custom handler to compose the Page & Archive data together? is there another way to access the Events Page data other than source.get('/events') ?
handler example: (adapted from three-bunnies/handlers.js at master Ā· annabranco/three-bunnies Ā· GitHub - thanks @annya.branco !)
// get all events
// note it actually doesn't use our archive slug from WP (set as "event-archive" in CPT UI)
const eventsList = await libraries.source.api.get({endpoint: "event"})
// get "events" page itself.
// this is where we will get page content from to show above our events list
const eventsPage = await libraries.source.api.get({endpoint: "pages", params: { include: 11 }});
thanks for any clarification
J