ryan Posted December 29, 2010 Share Posted December 29, 2010 I really need to create pagetype, that will redirect me to first of it's children... I.E. - Page 1 {/page1/} - Child 1 {/page1/child1/} - Child 2 {/page1/child2/} and when you click on 'Page 1' in menu, you'll go straight to '/page1/child1/'. For pages that you want to automatically direct to the first child, I would make them use a template that has this in it's code: <?php if($page->numChildren) $session->redirect($page->child()->url); And then, when you add another child, the internal pointer will redirect there. If you add a new child page, and they aren't sorted by date added (descending) then the newly added page isn't likely to be the first child. If no default sort is selected, then it'll add it as the last child. So if you wanted to redirect to last page (rather than the first) then you'd want to do this: <?php if($page->numChildren) $session->redirect($page->child("sort=-sort")->url); By the way "sort" is just a name representing the order that pages have been dragged/dropped to. That's the default value if a page doesn't have another field selected as it's default sort field. The minus sign "-" in front of it represents descending sort. Without the minus sign, it would be ascending. Since you want to select the last page, that's why it's "-sort" rather than "sort". Or if you wanted to make it select the most recent page added by date: <?php if($page->numChildren) $session->redirect($page->child("sort=-created")->url); The above is also descending since most recent added would be the highest date. 7 Link to comment Share on other sites More sharing options...
totoff Posted March 1, 2013 Share Posted March 1, 2013 Hi Ryan, I wonder if this technique may have any negative effect on search engines as it doesn't return a 301 that can be understood by the bots? What is your opinion/experience? Best, Christoph Link to comment Share on other sites More sharing options...
Wanze Posted March 1, 2013 Share Posted March 1, 2013 Hi totoff, It does set header 301 if you use Session::redirect by default. Here's the method from the Session class: <?php public function ___redirect($url, $http301 = true) { // if there are notices, then queue them so that they aren't lost $notices = $this->fuel('notices'); if(count($notices)) foreach($notices as $notice) { $this->queueNotice($notice->text, $notice instanceof NoticeError ? 'error' : 'message', $notice->flags); } // perform the redirect if($http301) header("HTTP/1.1 301 Moved Permanently"); header("Location: $url"); header("Connection: close"); exit(0); } 2 Link to comment Share on other sites More sharing options...
totoff Posted March 2, 2013 Share Posted March 2, 2013 hi wanze, thanks pretty much for your reply. as i'm a beginner with php and in particular with object orientated programming could you give me a hint how to implement this? do i have to "hack" the core (i.e. amend the session class)? best, christoph EDIT: read twice through your reply and think i got it on the second try ;-) pw is setting 301 header by default, right? no need to edit anything. 1 Link to comment Share on other sites More sharing options...
SiNNuT Posted March 2, 2013 Share Posted March 2, 2013 you got it right christoph 1 Link to comment Share on other sites More sharing options...
alan Posted September 5, 2013 Share Posted September 5, 2013 Ryan. Thank you so very much for if($page->numChildren) $session->redirect($page->child()->url); here. Totally rusty from far too long away from PW/PHP coding I was staring into space trying to work out how to do this. Until, that is, I searched for redirect page and your post came up... 1,000 thanks. Link to comment Share on other sites More sharing options...
hellomoto Posted February 1, 2015 Share Posted February 1, 2015 According to Pingdom on shared hosting this costs 7 seconds on an initial page load... Link to comment Share on other sites More sharing options...
salepg Posted February 2, 2015 Share Posted February 2, 2015 Hi, What's the difference between $page->child and $page->child() and when I should use which one? I know, noob question... but I had a problem with other usages not just at $page... :/ Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 2, 2015 Share Posted February 2, 2015 There's really no difference. $page->child internally calls $page->child(). You really only need to use the function if you want to specify additional selectors like this: $page->child("template=basic-page"); // First child matching this selector 1 Link to comment Share on other sites More sharing options...
salepg Posted February 2, 2015 Share Posted February 2, 2015 TY, LostKobrakai. Link to comment Share on other sites More sharing options...
Moebius Posted July 23, 2015 Share Posted July 23, 2015 My approach is to create page checkbox field named "redirects_to_first_child" and then execute redirect if this is true: <?php if($page->numChildren && $page->redirects_to_first_child) $session->redirect($page->child()->url); ?> This way I have full control over redirects via PW. Also absolutely love the fact it does 301 redirect. Link to comment Share on other sites More sharing options...
Rudy Posted November 9, 2016 Share Posted November 9, 2016 I can't get this method to work on a multilingual site On my default English site, it works fine. /news/ redirects to its first child, which is /news/2016/ but when I am on the French version /fr/news/ redirects to a blank page. Is it just me or anyone experience the same issue? Thx Rudy Link to comment Share on other sites More sharing options...
adrian Posted November 9, 2016 Share Posted November 9, 2016 Hi @Rudy, It's working for me here on a ML site. Is it actually redirecting to a blank page, or not redirecting at all and just showing no content? Is $page->child()->url returning the correct url to the first child? Link to comment Share on other sites More sharing options...
Rudy Posted November 9, 2016 Share Posted November 9, 2016 It's not redirecting at all. var_dump output returns string(0) "" Link to comment Share on other sites More sharing options...
adrian Posted November 9, 2016 Share Posted November 9, 2016 Bit of a longshot, but any chance the 2016 page is not activated for "fr" ? I get the same result as you if I uncheck the "active" checkbox on the settings tab. 2 Link to comment Share on other sites More sharing options...
Rudy Posted November 9, 2016 Share Posted November 9, 2016 Spot on @adrian! That was it. Thanks! 1 Link to comment Share on other sites More sharing options...
daclarkie Posted April 11, 2017 Share Posted April 11, 2017 On 3/1/2013 at 0:53 PM, Wanze said: Hi totoff, It does set header 301 if you use Session::redirect by default. Here's the method from the Session class: <?php public function ___redirect($url, $http301 = true) { // if there are notices, then queue them so that they aren't lost $notices = $this->fuel('notices'); if(count($notices)) foreach($notices as $notice) { $this->queueNotice($notice->text, $notice instanceof NoticeError ? 'error' : 'message', $notice->flags); } // perform the redirect if($http301) header("HTTP/1.1 301 Moved Permanently"); header("Location: $url"); header("Connection: close"); exit(0); } Using javascript works for me. Include following code in template file: <?php //If portfolio page make first child the current page $portfoliopage = $pages->get(1019); $posts = $pages->find("parent=$portfoliopage, sort=title"); if($page == $portfoliopage): $url = $posts->first()->url; ?> <script type="text/jscript"> location.replace("<?= $url ?>"); </script> <?php endif; ?> You can sort the posts array any way you like. For the latest addition use "sort=-sort" Success! Link to comment Share on other sites More sharing options...
daclarkie Posted April 11, 2017 Share Posted April 11, 2017 HI folks, hope this looks better. Using javascript works for me. Include following code in template file: <?php //If portfolio page make first child the current page $portfoliopage = $pages->get(1019); $posts = $pages->find("parent=$portfoliopage, sort=title"); if($page == $portfoliopage): $url = $posts->first()->url; ?> <script type="text/jscript"> location.replace("<?= $url ?>"); </script> <?php endif; ?> You can sort the posts array any way you like. For the latest addition use "sort=-sort" Success! 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