Hi, I would like to add "subpages" for a content template.
Basically there will be a tabbed navigation on the content template. When one of the headers is clicked, I want to redirect users to another page where the related information is displayed.
This is a basic version of how it will look like: hidden link
Every tab will contain data from the database so I don't want to load everything once the page loads. That's the reason I am thinking about redirecting the users to a different "page" when they click the link.
example:
myurl.com/company/company-slug -> this is the main page for the company
myurl.com/company/company-slug/reviews -> this is the page where I display the company reviews
myurl.com/company/company-slug/events -> this is the page where I display the company's active events
What is the best way for me to achieve this?
Thanks in advance
Hi,
Thank you for contacting us and I'd be happy to assist.
The most logical approach that won't require too much code customization or workarounds would be:
1. You can make the 'company' custom post type hierarchal so that each post in it, can have sub-posts.
For example:
- Company A
-- Company A Reviews
-- Company A Events
- Company B
-- Company B Reviews
-- Company B Events
- Company C
-- Company C Reviews
-- Company C Events
....
This will give you the URL/sub-page structure that you're looking for.
2. For the content templates, you can create a separate content template for each hierarchal level too:
a). CT for Company
b). CT for Company - Reviews
c). CT for Company - Events
The 'CT for Company' will be the main content template that will be assigned to this 'company' post type.
For the other two, they can be forced, through a custom function attached to the 'wpv_filter_force_template' hook:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template
That custom function will check if the 'review' or 'event' level company post is being viewed and will override the assigned content template, accordingly.
I hope this helps and please let me know if you need further assistance.
regards,
Waqar
Hi Waqar,
Thank you for your support. Since I have created the custom post types already and it might cause issues if I set the hierarchy now, I can try the second approach you recommended. Is there any example I can follow or use as a reference?
Thanks for writing back.
I suggested only a single approach in my last reply and those two are the steps for it. Both will be needed for this to work.
> Since I have created the custom post types already and it might cause issues if I set the hierarchy now,
- I don't foresee any issue in changing this post type setting. But you're welcome to make a complete back-up copy of the website, just to be extra cautious.
> Is there any example I can follow or use as a reference?
- I'm afraid, I don't have a reference website or code example available for this. But you can follow the steps suggested in my last reply and then if you face any challenge, feel free to share temporary admin login details of the website.
I'll be able to guide you with the next steps accordingly.
Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.
Hi Waqar,
I have thousands of events created in the system. If we are to create a parent\child relationship, does that mean I need to set that relationship for all existing records? Also I have a custom relationship between companies and events, would that be absolute then?
[edit] For some reason, the information I shared here is not private and visible by everyone. So I had to edit my message to remove the login information
Thanks for writing back.
In the approach that I suggested earlier, the 'Reviews' and 'Events' details won't be saved in the form of separate custom post types. They'll be part of the same 'Company' post type but in the form of hierarchal posts.
From what you've described, it looks like the Events are already added as a separate post type. If you'd like to keep them that way, then my suggested approach won't work.
Alternatively, you can keep the 'Reviews' and 'Events' in their separate post types, and then link them with the 'Company' post type, through one-to-many relationships:
a). Company -> Reviews
b). Company -> Events
Next, you can register custom endpoints for the 'Company' post type, for the '/reviews/' and '/events/':
https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
Here is a useful guide on the topic of registering custom URL endpoints in WordPress:
hidden link
Once these endpoints are working, you can use the 'wpv_filter_force_template' hook, to detect which endpoint is in use and then override the relevant content template, accordingly:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_force_template
For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/