I want to add a prefix to page URLs based on the page template and update also permalinks in the pages.

Current URL:


Expected result:


This rule should be applied only for template-market.php.

  • When the URL's are generated, in general WordPress doesn't know what templates theme will use to render the content. You would need to hook into the functions that generate URL's, and write the complete logic that will translate specific page URL is generated for to the templates you want to use. – Milan Petrovic Mar 26 at 12:16
  • 1
    the template doesn't determine the URL, it's the other way around! The URL determines what gets loaded, and what gets loaded is then used to figure out which template to use. Instead of using page templates for everything, would it not be easier to create a custom post type named market and use single-market.php? Set its archive url to /m/ and hey presto you get what you want and you get a markets menu in the WP Admin so it's easier to use – Tom J Nowell Mar 26 at 12:31
  • @TomJNowell But when I create a page there is a field to choose a template. The rule which you wrote, works also for the standard WordPress pages? – Dawid Malec Mar 26 at 12:36
  • I don't understand what your last comment is trying to convey. Your mental model of how URLs and templates work is the wrong way around. URLs determine the query, query determines the template. You can't work back from the template to the URL, that's not how WordPress works. Instead, of making all pages with that template have a URL, why not make all pages with that URL have a template? – Tom J Nowell Mar 26 at 12:59
  • Thanks for clarifying how Wordpress works in these cases! I think I will follow your advice and I will build a CPT. Thank you! – Dawid Malec Mar 26 at 13:25

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.