Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Alternative Redirect to First Child use


Go to End


20 Posts   11985 Views

Avatar
tazzydemon

Community Member, 135 Posts

18 February 2012 at 9:58am

Its easier if you just stick this at the end of your page controller init function rather than use the template



public function init() {
parent::init();

        if($this->RedirectToChildOne && $this->Children()->Count()){
        Director::redirect($this->Children()->First()->AbsoluteLink());

This feature is invaluable if one is using jquerymobile since otherwise the root page is invisible and its content has to be moved to be its own first child

Avatar
NickJacobs

Community Member, 148 Posts

20 March 2012 at 11:14am

Edited: 20/03/2012 11:15am

Hi, just in case anyone wants a variation...I just test to see if the page has any content, then redirect to the first child if not.

if(!$this->dbObject('Content')->hasValue()){ 
    if($this->Children()->Count()){ 
    Director::redirect($this->Children()->First()->AbsoluteLink()); 
    } 
}

Avatar
Friizu

Community Member, 17 Posts

12 December 2015 at 3:27am

As Director::redirect() is deprecated, this is what i used:

public function init() {
   parent::init();
      if($this->Children()->Count()){
      return $this->redirect($this->Children()->First()->Link());
   }
}

Avatar
Friizu

Community Member, 17 Posts

15 December 2015 at 10:38pm

Code i added works but it affects also admin so at the end i solved this issue in template so that's what i added to my menu code in Navigation.ss:

<% if $Children %>
<a href="$Children.First.Link" title="$Title.XML">$MenuTitle.XML</a>
<% else %>
<a href="$Link" title="$Title.XML">$MenuTitle.XML</a>
<% end_if %>

Go to Top