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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Showing category articles on specific pages, Need help.


Go to End


3 Posts   1134 Views

Avatar
jamesmburnett

Community Member, 3 Posts

2 June 2016 at 6:06pm

Edited: 02/06/2016 6:25pm

Hi Mates,

Ive been following the tutorials here: https://www.silverstripe.org/learn/lessons/ for creating an article system
which has been great. Got it working and very happy.

Ive then gone on to add the homepage controller which will show the 'Latest news' from the article page holder.
Which again has worked great.

My question is:

I have a number of different pages which relate to the categories on the article page. I would like to just to show the articles relating there category on different pages

EG:

Apartment Development Page
---------> Displays the latest 3 articles from the News page with the category Apartment Development on to the Apartment Development Page

Retire Page
---------> Displays the latest 3 articles from the News page with the category Retire on to the Retire Page

ATM the Apartment Develeopment page will show all of the articles(Apartment Development, Retire, Home & Land etc). I need it to just show the Apartment Development Articles on the Apartment Development Page. So on for the other pages.

ApartmentDeveleopment.php
---------------------------------------------------------------

class ApartmentDeveleopment_Controller extends Page_Controller{

		public function LatestArticles($count = 3){
			return ArticlePage::get()
				-> sort('Created', 'DESC')
				->limit($count);

		}

	
	}

I am new to Sliverstripe and slowly learning this cool CMS. If some one could point me in the right direction i would be VERY graeteful!
Many Thanks

Avatar
martimiz

Forum Moderator, 1391 Posts

4 June 2016 at 2:28am

Assuming 'Category' is a textfield on the Page, something like:

		public function LatestArticles($count = 3){
			return ArticlePage::get()
				->filter(array('Category' => 'Apartment Development'))
				->sort('Created', 'DESC')
				->limit($count);

If you set it up using a Category DataObject, with Category has_many Pages, then something like:

		public function LatestArticles($count = 3){
			return ArticlePage::get()
				->filter(array('CategoryID' => '3'))
				->sort('Created', 'DESC')
				->limit($count);

etc.

Avatar
jamesmburnett

Community Member, 3 Posts

6 June 2016 at 11:31am

Edited: 06/06/2016 12:13pm

Thank you so much

I got it working with the below code.


public function LatestArticles($count = 3){
			return ArticlePage::get()
			->filter(array('Categories.ID' => '1'))
			-> sort('Created', 'DESC')
			->limit($count);

		}