I have a view with multiple food items. Each item belongs to category and a subcategory (i.e. Beverages→Tea, Beverages→Coffee etc.). With Views I've created a search form that selects all items from particular category and a subcategory. This view is uses AJAX to update it's content. Works really great!
What I need to do is to display a search category and a subcategory inside a page title. So if someone searched for 'Tea' and selected 'Beverages→Tea' in a search dropdown, a title would show 'Beverages: Tea'.
Since it uses AJAX I think that any PHP techniques (i.e. shortcodes) are out of use here. So I think it has to be a JS-only solution.
What I'm planing is:
1. Determine what category slug was searched by
1a. Checking search drop down for options with 'selected' attribute, or, probably better yet
1b. Parse a URL string for a URL search parameter to figure out what term was searched for.
2. Using WP REST API to retrieve a list of all terms for a category. Scan the list for the slug that was searched. Check if this slug has any parent items, and if it does, retrieve those parent items. Continue until the very top parent item is found.
3. Use JS to replace page title text with parent and child categories.
4. Repeat every time a search is executed.
Is this the right approach to achieve what I want? Are there any better or simpler ways to achieve the same?
Thank you very much, Minesh!
This is exactly what I need.
May I have a suggestion for some time in the future to pass search terms as parameters to the data object of a callback function? This will allow to avoid parsing the URL and be future proof in case URL parameters format change. I think it shall not be hard to implement.
Thanks!
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.