Solutions

News page ordered by date

19 October 2009

This tutorial explains how to create a news page that automatically orders by date using new features in Perch 1.2.

We start by creating our news page. On the page that will display the news add your Perch tag, giving it an ID of ‘News’.

News 1: add the Perch tags to the news page

Refresh the page and log into the administration area. Select the News region which should be marked as new.

News 2: the new news region

I am just going to use the Article template which is included with Perch, so in the drop down select Article and check the allow multiple checkbox.

You can now add some news items. Add a few items for testing each with a different date.

News 3: select the article template

After adding a few items refresh your news page to see the items listed. You will see that the items appear in the order that you entered them and are not sorted by date. In Perch versions prior to 1.2 the only ordering option that you had was to choose whether new items went in at the bottom or the top of the list. With Perch 1.2 you can select an item to order by – in this case the date.

Return to the admin and select your News region again. In the sidebar click the “Set your options for this region” link.

News 4: set opions link

This screen lets you set some basic options for the region, including selecting one of the fields to order by and the order that the ordering runs. In our case we can select Date and Descending so that items display on the page with the newest first.

You could also select to only show a certain number of items here, so that if the administrator adds a lot of items the page will only show the number that have been set.

News 5: setting options fo this region

Save these settings and refresh your news page to see the order change.

Creating a custom sort order

You can create a sort field that does not show on the page to enable a user to create their own sort order for a region. In your template add the following tag:

<perch:content id="order" type="text" label="Order" suppress="true" />

This will show up in the admin and can be used to enter a number which indicates the sort order of the items. In Settings for the Region you can then choose to sort by that field. The attribute suppress being set to true means that this field will only display in the admin area and not in the page.

Files for this solution are available to download. Just drop news.php into your site to try this solution for yourself.

Posted by Rachel Andrew at 14:38
Tagged: