Chris Whissen Posted February 17, 2017 Share Posted February 17, 2017 I'm not even sure where to start here. I'm trying to find the best way to do this... Basically, I have a page Services. This page has a number of children which represent sections of the Services page. So, the tree is: Home - Services -- Thing1 -- Another2 -- LastOne3 Services has a template that renders each child page as a separate Div - allowing for flexiblity in layout and easy editing. On the home page, I have several links that relate specifically to Thing1, Another2, and LastOne3. If I was hardcoding those links, I would just do href="services.html#Thing1" to link directly to the proper section. How can I do that in ProcessWire? My first thought is to do a two part selection where they select the parent page (i.e. sevices) and then the child page (i.e. Thing1). I'd then have the childpages use their title as a section id, and then pull the two links in separately. Something like: echo href='{$parentlink}#{$childlink}' (That's simplified, just to get point across) The problem is it requires the user to select two things to get to the link right. Is there a way I can allow the user to just select the child page and still output the jump link? As an additional twist, the links on the home page are part of a repeater if that matters. Using ProcessWire 3.0.42. My PHP and ProcessWire skills are still in early stages of development, so please take that into consideration. Thanks in advance for any ideas or help. Link to comment Share on other sites More sharing options...
Macrura Posted February 17, 2017 Share Posted February 17, 2017 (edited) One way to do it is is with a redirect from the child page to the parent page, kinda hackish but works: /* Check for Redirect -------------------------------------*/ if($page->parent->template == 'service') { $r = ($page->parent->url . "#" . $page->name); $session->redirect($r); } then you just use the $page->url for the sections. a better way would be to use a hook and check the type of page template and then change the URL to the hash: wire()->addHookBefore('Page::path', function($event) { $page = $event->object; if($page->template == 'service-child') { $event->replace = true; $hashUrl = $page->parent->path . '#'. $page->name; $event->return = $hashUrl; } }); Edited February 17, 2017 by Macrura fixed per Robin S correction. 1 Link to comment Share on other sites More sharing options...
Robin S Posted February 17, 2017 Share Posted February 17, 2017 What Macrura said, but with one small change: $hashUrl = $page->parent->path . '#'. $page->name; Edit: or maybe better... $hashUrl = $page->parent->url . '#'. $page->name; ...in case you have installed PW in a subdirectory. 1 Link to comment Share on other sites More sharing options...
Chris Whissen Posted February 17, 2017 Author Share Posted February 17, 2017 Thank you both I'll give it a try this weekend. Link to comment Share on other sites More sharing options...
Chris Whissen Posted February 21, 2017 Author Share Posted February 21, 2017 So, thanks again for the ideas. I ended up going a slightly different way. I'm not sure is the most efficient, but it works and it's easy for the user. The relevant pieces of what I'm using(there are more parts to the repeater that aren't relevant here, but are why I used a repeater): <?php foreach($page->threewide as $three) { $idlink = $pages->get("$three->sp_link"); echo "<a href='".$idlink->parent->name."#".$idlink->name."'>";} ?> Since there are multiple template options, I have the page field limited to the children of the services page. I need to learn how to use that filedtype better. So, it's not perfect if they decide to link to multiple subsections. However, it serves their current needs which lets me move forward and figure out a better solution later. Perhaps after I've gotten the hang of hooks and such. Thank you both! 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