wesp Posted February 13, 2020 Share Posted February 13, 2020 Hi everyone, My setup is that I have a page tree like home = articles == article-1 == article-2 On the home page I link to the individual articles and I'd like the url to be page.com/article-1 instead of page.com/articles/article-1 I guess this is a kind of old topic, because I want to achieve the same as in these: so far I've tried putting either one of the following hooks into the _init.php: $pages->addHookAfter('Page::path', null, 'hookPagePath'); function hookPagePath(HookEvent $e) { $page = $e->object; if($page->template == 'article-page') $e->return = "/$page->name/"; } $pages->addHookBefore('Page::path', function($event) { $page = $event->object; if($page->template == 'article-page') { $event->replace = true; $event->return = "/$page->name/"; } }); and they both actually work in the way that the links on the home page do and look like I want, but when I follow the links they just redirect me to the home page again. As in they don't even throw a 404 anymore when I put in some random letters in the url, I always get redirected to home. And that seems to be caused by allowing URL Segents in the home template, because if I uncheck it I get 404s when following the links. I don't quite get how to work with this as in all other forum topics no one else seems to have had that problem, so any help is much appreciated! Link to comment Share on other sites More sharing options...
Zeka Posted February 13, 2020 Share Posted February 13, 2020 @wesp You have to turn on URL segments on home page template and in your template file if (input()->urlSegment(1)) { $pagename = input()->urlSegment(1); $match = pages()->findOne("template=article-page, name={$pagename}"); if (!$match->id) throw new Wire404Exception(); echo $match->render(); return $this->halt(); } Link to comment Share on other sites More sharing options...
wesp Posted February 13, 2020 Author Share Posted February 13, 2020 Hi @Zeka thanks for the fast reply! I made sure to do that and at first it then gave me this error: After putting $config->useFunctionsAPI = true; inside the config.php it still doesn't really work, as in the links look right and clicking them does redirect me to a new page but that page is completely empty and not rendered via the template file article-page Link to comment Share on other sites More sharing options...
dragan Posted February 13, 2020 Share Posted February 13, 2020 @wesp You don't need the Functions API for the functionality you asked for - it's just the syntax Zeka used that requires it, e.g. pages() instead of $pages etc. I have tried this combination here, and it works fine: // at the very top of home.php if ($input->urlSegment1) { $pagename = $input->urlSegment1; $match = $pages->findOne("template=article, name=$pagename"); if (!$match->id) { throw new Wire404Exception(); } echo $match->render(); return $this->halt(); } // site/ready.php // taken from this great case study: https://processwire.com/talk/topic/3987-cmscritic-development-case-study/ wire()->addHookBefore('Page::path', function($event) { $page = $event->object; if($page->template == 'article') { $event->replace = true; $event->return = "/$page->name/"; } }); If you copy-and-paste, pls note that my template is called 'article', not 'article-page' ? Attached also the home template URL segment settings. 1 1 Link to comment Share on other sites More sharing options...
wesp Posted February 14, 2020 Author Share Posted February 14, 2020 @dragan that did the trick, thanks! Link to comment Share on other sites More sharing options...
wesp Posted February 14, 2020 Author Share Posted February 14, 2020 @dragan maybe you can also help with another issue I have since it's a very similar thing but for a different website: I have a one-pager with the children pages being rendered as sections with their own template file, each section has the $page->name as id so I can link to them as anchor links. The case here is that I'd like the links to not have the hash symbol, so that mywebsite.com/section1 acts like mywebsite.com/#section1 but doesn't look like it. I tried doing the same for this as what worked with hiding the parent pages from the url, modifying it a bit because of the multiple templates, but i am at a loss if that even makes sense to try to do here, because that didn't work. Link to comment Share on other sites More sharing options...
dragan Posted February 14, 2020 Share Posted February 14, 2020 @wesp Well, that ain't so easy. If you wanted to make it behave like that - and make sure you don't break user's back-button in the process, and make sure people actually scroll to the section if they share/bookmark such a pseudo-link - you should use an SPA framework like Vue instead, where you have such custom routing functionality already built-in. otoh, it's quite easy to hide / remove the #section1 completely from the location bar - but that's not what you're after. 2 Link to comment Share on other sites More sharing options...
wesp Posted February 17, 2020 Author Share Posted February 17, 2020 @dragan thanks for the info, I will look into that! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now