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

Display Content of a page in Footer


Go to End


4 Posts   1631 Views

Avatar
sebastiankozub

Community Member, 59 Posts

27 October 2015 at 10:15pm

Hello,

I'd like to display in Footer:
A) content (or other fields) from a page with a given name (let's say I'd like to display contact information from the ContactPage named "Kontakt")
B) content (or other fields) from a first (or random) page of a given type (let's say I'd like to display content from random (or first) page of ProductPage type)

A

public function KontaktContent() 
{
    return $contactPage = ContactPage::get()->First();  // prefer to get a page by name because there can be more than one page of ContactPage type
                                                                                      // also want to get Content only, not a whole page 
}

B

public function RandomProductPage() 
{
    return $randomProductPage = ProductPage::get()->Random();   // is it correct?
}

Thanks in advance, Sebastian

Avatar
martimiz

Forum Moderator, 1391 Posts

27 October 2015 at 11:47pm

Edited: 27/10/2015 11:48pm

You can access a specific page directly from within the CMS using its URLSegment, doing

<% with $Page(my-page) %>
    $Title
<% end_with %>

But as the URLSegment may change, you could perhaps better use the Contact page by its ID (which is unique, and will probably remain the same):

return ContactPage::get()->byId(12);

You can easily get the ID by hovering over the page in the CMS :)

If you absolutely want to filter by Page Title, do this:

return ContactPage::get()->filter('Title', 'Contact')->first();

As to getting a random page - I don't know of any function random() - it may still exist - but i think you can:

return ProductPage::get()->sort('RAND()')->first();

Avatar
sebastiankozub

Community Member, 59 Posts

11 December 2015 at 7:49am

return ProductPage::get()->sort('RAND()')->first();

How can I get only the content of the randomly chosen page with the code above?

Avatar
martimiz

Forum Moderator, 1391 Posts

11 December 2015 at 7:58am

Edited: 11/12/2015 11:33pm

Something like this...

public function getRandomProductPage() {
    return ProductPage::get()->sort('RAND()')->first();
}

<% with $RandomProductPage %>
    $Content
<% end_with %>