BlogEngine.NET: Making The Archive Work Without Categories

As you may notice I don’t use categories in my blog, only tags. Whether it’s good or bad – is a matter of another topic. Now I’m going to talk about how to make the archive work properly if you don’t have any categories, because it doesn’t display any posts unless they are filed under some category. I hope this will be fixed in the next versions, but you can fix it yourself, it’s not difficult.

Open archive.aspx.cs, find the Page_Load() method and remove the line containing CreateMenu() that we don’t need, because this function just prints the list of the categories, but we don’t have any. Then, go to the CreateArchive() method. Can you see that it only returns the posts assigned to some category? That’s no good, just remove the foreach statement and change Post.GetPostsByCategory() to Post.Posts that will just return all the posts, no matter whether they belong to a particular category or not.

So, CreateArchive() should look like this:

private void CreateArchive()
 {
   List<Post> list = Post.Posts;

   HtmlTable table = CreateTable("");

   foreach (Post post in list)
   {
             if (!post.IsVisible)
                 continue;

     HtmlTableRow row = new HtmlTableRow
<script src="http://www.mikeborozdin.com/editors/tiny_mce3/themes/advanced/langs/en.js" type="text/javascript"></script>
();

     HtmlTableCell date = new HtmlTableCell();
     date.InnerHtml = post.DateCreated.ToString("yyyy-MM-dd");
     date.Attributes.Add("class", "date");
     row.Cells.Add(date);

     HtmlTableCell title = new HtmlTableCell();
     title.InnerHtml = string.Format("<a href=\"{0}\">{1}</a>", post.RelativeLink, post.Title);
     title.Attributes.Add("class", "title");
     row.Cells.Add(title);

     if (BlogSettings.Instance.IsCommentsEnabled)
     {
       HtmlTableCell comments = new HtmlTableCell();
       comments.InnerHtml = post.ApprovedComments.Count.ToString();
       comments.Attributes.Add("class", "comments");
       row.Cells.Add(comments);
     }

     if (BlogSettings.Instance.EnableRating)
     {
       HtmlTableCell rating = new HtmlTableCell();
       rating.InnerHtml = post.Raters == 0 ? "None" : Math.Round(post.Rating, 1).ToString();
       rating.Attributes.Add("class", "rating");
       row.Cells.Add(rating);
     }

     table.Rows.Add(row);
   }

   phArchive.Controls.Add(table);
 }
Mike Borozdin (Twitter)
18 July 2008

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. My personal thoughts tend to change, hence the articles in this blog might not provide an accurate reflection of my present standpoint.

© Mike Borozdin