As a result, I used the datefolders package, which automatically sorts the news in the correct folder of the year and month based on a predefined date field, as @amelvin's answer . The advantage of the finalists is that it makes it easy for the user to manually sort their articles in the correct folder and maintain this organization. They can simply right-click a news container item, create an article, set a date, and it will only appear in the right folder. In addition, it changes the folder when the date changes.
In terms of displaying archives, I had the following razor code, where NewsListing is the document type for news items, and NewsItem is the document type for the news item:
Archive list in the sidebar
<umbraco:Macro runat="server" language="cshtml"> @{ dynamic newsListingNode = Model.AncestorOrSelf(1).DescendantsOrSelf("NewsListing").First(); } <div class="archive"> <ul> @foreach (var newsYear in newsListingNode.Children) { foreach (var newsMonth in newsYear.Children) { @* Use format time to get the month string *@ dynamic dateLabel = umbraco.library.FormatDateTime(newsYear.Name + " " + newsMonth.Name + " 01", "MMMM yyyy"); <li><a href="@newsMonth.Url">@dateLabel»</a></li> } } </ul> </div> </umbraco:Macro>
Month archive page
<umbraco:Macro runat="server" language="cshtml"> @* Check the it is a month folder *@ @if ((@Model.NodeTypeAlias == "NewsDateFolder") && (@Model.Up().NodeTypeAlias == "NewsDateFolder") && (@Model.Up().Up().NodeTypeAlias == "NewsListing")) { dynamic newsMonth = Model; dynamic newsYear = Model.Up(); dynamic dateLabel = umbraco.library.FormatDateTime(newsYear.Name + " " + newsMonth.Name + " 01", "MMMM yyyy"); <div class="news"> <h2>News archive: @dateLabel</h2> @{ dynamic newsItems = Model.DescendantsOrSelf("NewsItem").OrderBy("sortDate desc"); } @foreach(var newsItem in newsItems) { <div class="block-content"> <h5><a href="@newsItem.Url">@newsItem.Name</a></h5> <p>@newsItem.summaryText</p> <a href="@newsItem.Url">more»</a> </div> } </div> } </umbraco:Macro>
source share