@{ //current month value to compare string month=String.Empty; var nodes =Model.Descendants().Where("Visible") .OrderBy("createDate")
@foreach (var item in nodes)
{ //if the month changed to another month change month header @if(!month.Equals(item.createDate.ToString("MMMM"))) { month=item.createDate.ToString("MMMM"); @month
Group pages by Month in creationDate and loop foreach?
I want to make a list with all my news items where the posts are grouped by month so I got:
January
February
March
....
I know how to make a foreach loop with all Children but I have no clue how I make a distinct selection by only the month.
Can you please show us what your nodestructure looks like at the moment?
News
- item 1
- item 2
or maybe
News
- 2011-10-02
- item 1
- 2011-09-29
- item 2
Sure! It looks like this:
News
- item 1
- item 2
- item 3
- etc...
NiQ,
Could you try out this.
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
//current month value to compare
string month=String.Empty;
var nodes =Model.Descendants().Where("Visible")
.OrderBy("createDate")
@foreach (var item in nodes)
{
//if the month changed to another month change month header
@if(!month.Equals(item.createDate.ToString("MMMM")))
{
month=item.createDate.ToString("MMMM");
@month
}
@item.title
@item.description
}
}
Change the properties accordingly.
Thanks,
Rajeev
is working on a reply...