Jump to content

A question about URLs / page names


mjut
 Share

Recommended Posts

Hello!

I am trying to get a self generated name for new pages containing the creation-date and the title.
At the moment, I am having a template set up with a name format for children like so:
date:Y-m-d

It works fine, the page name is generated and shows the URL like this
domain.com/2024-11-25/

Is it possible to write the title name to the page name too? Like so:
domain.com/2024-11-25-hello-world/

I cannot find a simple solution to it in the forums... I might just be looking at the wrong places? It seems so simple, but I just cannot get it working....
Thanks for some input/thoughts!

Link to comment
Share on other sites

Cool! Thank you @Nicolas !

I did modify that code slightly. I put it into ready.php:

wire()->addHookBefore("Pages::published(template=article)", function($hook) {
  $page = $hook->arguments(0);
  if($page->skipMyHook) {             // if the page already has our temporary property, we skip further processing
    return;
  }
  $newUrl = date("Y-m-d-") . wire()->sanitizer->pageName($page->title) ; // give it a name used in the url for the page
  wire()->log->message($page->title);
  $page->skipMyHook = true;           // add a temporary property to the page (it is only in memory)
  $page->setAndSave('name', $newUrl);
});


 

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...