Jump to content

Removing Parents from url


wesp
 Share

Recommended Posts

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

@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

Hi @Zeka 
thanks for the fast reply! I made sure to do that and at first it then gave me this error:

1112741856_Bildschirmfoto2020-02-13um16_32_03.thumb.png.d4a1aee0cd457b04f1511c1e03b441de.png

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

@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.

Screenshot_2020-02-13 Edit Template Home (home) • pwbig test.png

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

@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

@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.

  • Like 2
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...