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.

Archive /

Our old forums are still available as a read-only archive.

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

Random Landing Pages


Go to End


2 Posts   2497 Views

Avatar
workshed

Community Member, 4 Posts

1 October 2008 at 9:36am

Hi-

We have a site with different landing pages, which are being used to track ad responses from some tv commercials and print ads. The home page of the site currently uses one of the landing pages as the home page, but we'd like to randomly rotate between the three pages.

So, for example:

domain.com/landing1
domain.com/landing2
domain.com/landing3

How can we, when loading the home page, randomly choose one of those to which we can redirect?

Thanks for any suggestions!

-Bret

Avatar
Sean

Forum Moderator, 922 Posts

1 October 2008 at 12:02pm

When the user hits the domain, they'll go to the page "home", which should be set up as a custom page type - that being HomePage.

For the other pages, you'll want to give them a custom page type as well, something like LandingPage.

Here's example code:


class HomePage extends Page {

}

class HomePage_Controller extends Page_Controller {

	// Get a random LandingPage from the site tree and redirect to it
	function index() {
		$randomLandingPage = DataObject::get('LandingPage', '', 'RAND()', '', 1);
		if($randomLandingPage) {
			Director::redirect($randomLandingPage->Link());
		}
	}

}

Then, you need to create LandingPage as another page type and apply it to pages in the CMS site tree that you want to be candidates to be redirected to.

Cheers,
Sean