0

WordPress is installed in the domain's root. The site is built using custom page templates (not posts).

I'm familiar with this SO question Move pages to subdirectory in Wordpress.

I understand that I can create pages with a parent page assigned which make the page URL appear as if the page is located in a subdirectory of the domain. But the subdirectory is only a virtual one, from what I can tell.

If I create a physical subdirectory (with a name that matches the the parent), and copy a page template into the subdirectory, the page template in the subdirectory is not loaded. The page template in the theme's root still gets loaded.

My question is, does WordPress support the ability to have pages with the same name (but obviously different content) in different subdirectories? If so, how is this achieved?

Though the site has nothing to do with insurance, this is the URL structure we want to have (access to the contents of the subdirectories will be based on a user's role):

  insurance.com/life/calculator
  insurance.com/property/calculator
  insurance.com/auto/calculator

And no, this does not work for our purposes:

  insurance.com/life-calculator
  insurance.com/property-calculator
  insurance.com/auto-calculator
4
  • I'm confused, your questions title is talking about page templates, but then your questions text talks about URLs, can you edit your question to remove the contradiction?
    – Tom J Nowell
    May 3, 2023 at 11:42
  • @TomJNowell I guess that's my problem. I don't understand the difference. The page templates currently map 1-to-1 with a URL and are in the root of the custom theme. I now want to add subdirectories to the site's structure with each subdirectory having a calculator page for example. Each calculator is different. How do I create the page templates to achieve this since I can't have multiple page-calculator.php template files in the theme's root? Glad to make whatever edits to the question I should make.
    – Karl
    May 3, 2023 at 12:16
  • a page template is a physical PHP/HTML file in the theme
    – Tom J Nowell
    May 3, 2023 at 12:33
  • @TomJNowell yes, I understand that. Thus, "How do I create the page templates to achieve this since I can't have multiple page-calculator.php template files in the theme's root?" With "this" being same page names in different subdirectories. WordPress is not loading the page-calculator.php templates located in the subdirectories. It loads the template file in the theme's root directory.
    – Karl
    May 3, 2023 at 12:40

1 Answer 1

1

It sounds like you'll need to create the page templates with filenames such as tpl-calculator-1.php, tpl-calculator-2.php, etc. You then make sure to put a comment at the top of each template to give it a user-friendly name that will appear in the Editor:

<?php
/**
 * Template Name: Events Thank You
 */

Files named this way will not be automatically applied, so instead you'll edit each page (or custom post type) and manually choose the template. A dropdown will appear and you'll choose your user-friendly template name from the dropdown:

Screen shot of template dropdown

Another way to approach this is to create each section as a custom post type. So you'll have life posts, property posts, and auto posts. If you do it this way, it's extra easy to restrict each post type to only be edited by certain roles. You can also have these template automatically apply by naming them single-{posttype}-slug.php - for example, single-life-calculator.php and single-auto-calculator.php.

1
  • Thank you. For me, the missing piece was Template Name:
    – Karl
    May 3, 2023 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.