2

I am building a site with several sections, and one of them is a special page to navigate through categories in a hierarchical style (you can navigate into categories/subcategories and see their children). So I have added a Page and I have assigned a custom template to it in the Template drop-down.

Maybe what I want to do is not possible, or it's too tricky: I'd like to have my page, which URL is www.domain.com/tutorials to display a top menu with the main categories. This menu is a WordPress menu registered with register_nav_menus, with links to categories.

I'd like the items on the menu to point to that same page, with the slug of the category: www.domain.com/tutorials/my-category (instead of www.domain.com/category/my-category).

Each "category page" will show links to sub-categories that should have the same URL structure (i.e.: www.domain.com/tutorials/my-category/my-subcategory) I know how to query for sub-categories and get the current category with get_query_var, but again I have this problem producing the right URL for each one.

I've seen the pieces I need, like add_rewrite_rule(), add_rewrite_tag() or parse_request(). I can also have wp_nav_menu_objects and Walker_Nav_Menu but I can't put together a setup where my Tutorials page doesn't return a 404 if I add categories/subcategories to the URL (it only accepts going to www.domain.com/category/my-category).

I have been able to render a menu pointing to my page by getting each menu $item->url and replacing 'category' by my page's slug, so my only remaining issue is the 404 problem.

My WP settings are: Category base is empty, and my permalink structure is /%category%/%postname%/

Could anyone point me in the right direction?

  • 1
    Set your category base to tutorials and that will set all the URLs the way you want them. It would also be easier to maintain, and better practice, to set up your theme templates to handle the lists of categories. Instead of using a wp_nav_menu which you have to manually maintain, use wp_list_categories. Once you have your permalinks set the way you want, be sure to clear your cache or use an incognito window to check whether the new URLs are working. Once they are there are plugins to check which template file each page is using, so you can tell what file to modify for /tutorials/. – WebElaine Mar 13 '18 at 15:03
  • Thanks! That sounds good, although I have a couple of limitations. If I set my base category to 'tutorials', that will apply to all post types, including content in the blog and other sections. About wp_list_categories, it also sounds right, but my client wants their editors to decide which are the main categories to want on top of the Tutorials page. They will have different categories in the site, and only 4 or 5 of them will be in this specific page. Thanks anyway! – Luis Serrano Mar 13 '18 at 15:11
  • The menu limitation makes sense, but setting your base category should not apply to all post types, just Posts. Pages and CPTs will be unaffected (and if for some odd reason CPTs are affected, either you're using built-in Categories for them and can use a custom taxonomy instead, or else you need to change the CPT registration to set the permalinks as you need them). – WebElaine Mar 13 '18 at 15:49
  • 1
    I think the answer might be CPT actually. I want posts to be different from tutorials, so I will add a Tutorial post type and a template to list them with the hierarchy I described. Thanks for the suggestions and your time. – Luis Serrano Mar 13 '18 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.