Jump to content

Ignore a parent in URL (but allow grandchildren)


Recommended Posts


I want to keep sort some pages in my page tree within a single parent, but then have the URLs for those pages ignore their topmost parent.

So for example, if the page is at: [home]/buildings/architect-name/building-name
I want the URL to be /architect-name/building-name

I've search around the forum, and the solution that comes up everywhere is a version of the code from this post

 * This hook modifies the default behavior of the Page::path function (and thereby Page::url)
 * The primary purpose is to redefine blog posts to be accessed at a URL off the root level
 * rather than under /posts/ (where they actually live). 
wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->template == 'post') {
    // ensure that pages with template 'post' live off the root rather than '/posts/'
    $event->replace = true;
    $event->return = "/$page->name/";

And I can use a version of that to successfully get buildings/architect-name to appear as /architect-name
But /buildings/architect-name/building-name still appears as /buildings/architect-name/building-name

Is there a way to get URLs to ignore/remove that topmost parent even when the page has a grandchild?

Any help much appreciated!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...