Modify first page to only show a specific category

Topics: ASP.NET 2.0
May 14, 2010 at 1:13 AM
Hi How can I modify the first page to only show a specific category? I want only posts that are categirized as "news" to be displayed initially, then I have links people can click that shows the other categories. Someone? Please?
May 16, 2010 at 10:53 PM

Not very much activity in these "forums" apparently...

Anyone got some insight or pointers on this topic?

May 16, 2010 at 11:20 PM

I think you can accomplish this with some modifications to the PostView user control. Let me test something real quick on my development machine and I'll post a potential solution to this forum later on today.

May 17, 2010 at 12:29 AM

I've got some good news and bad news - the good news is that you can definitely modify the homepage to make it happen - all you have to do is make a custom version of the PostList.ascx user control that queries any posts which implement the IPublishable interface and contain the category or categories you're looking for and embed that one onto default.aspx instead of the standard PostList.ascx. The bad news is that I don't think this is something you can accomplish using the BlogEngine.NET theming engine - I spent the past hour trying to find a way to accomplish using a very jerry-rigged version of PostView.ascx this and I've come around to admit that I'm not familiar enough with the BlogEngine.NET themeing engine to pull it off.

Additionally, I don't know if the BlogEngine.NET themeing engine supports modifying a page like default.aspx or a user control that isn't CommentView.ascx or PostView.ascx. One of the project coordinators might be able to tell us if that's the case, but I'm just not sure :(

Coordinator
May 17, 2010 at 1:45 AM
Edited May 17, 2010 at 1:47 AM

If you want to display the News category posts on the homepage, you can do that by modifying default.aspx.cs (in the root folder).  In there now, you'll find this line of code (I added a line break for the purposes of posting here):

PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
	delegate(Post p) { return p as IPublishable; }));

Replace that with:

Category newsCategory = Category.Categories.Find(delegate(Category c)
	{ return c.Title.Equals("News", StringComparison.OrdinalIgnoreCase); });
if (newsCategory != null)
	PostList1.Posts = newsCategory.Posts.ConvertAll(new Converter<Post, IPublishable>(
		delegate(Post p) { return p as IPublishable; }));
else
	PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
		delegate(Post p) { return p as IPublishable; }));

May 17, 2010 at 2:02 AM

I was imagining something along those lines, but can this change be applied in a theme-specific way? Can a theme override default.aspx or is it constrained to just the masterpages and the CommentView and PostView controls?

Coordinator
May 17, 2010 at 6:11 AM

Hi Aaron, that's true, a theme cannot easily override the posts that default.aspx (the homepage) decides to output.

The theme could display posts (or a summary of posts, etc) in the sidebar, or even in the "middle" area above or below the content that default.aspx displays.

On second though, a theme might be able to, from the master page, use "FindControl" to get a handle on the PostList control that default.aspx uses, and then change the posts that are fed to PostList.  It's a little bit of a backwards approach, but would probably work.

May 18, 2010 at 4:24 PM
BenAmada wrote:

If you want to display the News category posts on the homepage, you can do that by modifying default.aspx.cs (in the root folder).  In there now, you'll find this line of code (I added a line break for the purposes of posting here):

PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
	delegate(Post p) { return p as IPublishable; }));

Replace that with:

Category newsCategory = Category.Categories.Find(delegate(Category c)
	{ return c.Title.Equals("News", StringComparison.OrdinalIgnoreCase); });
if (newsCategory != null)
	PostList1.Posts = newsCategory.Posts.ConvertAll(new Converter<Post, IPublishable>(
		delegate(Post p) { return p as IPublishable; }));
else
	PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
		delegate(Post p) { return p as IPublishable; }));

 Thank you Ben, this solution worked like a charm, exactly what I was looking for.

Also, thank you Aaron for trying to help out.

Jun 17, 2010 at 2:19 PM
Edited Jun 22, 2010 at 4:23 PM

Ignore

Jun 19, 2010 at 7:02 AM

I think this should be suggested to be added as a feature to BlogEngine 2.0. Anyone else agrees? 

Do we have document/list of features coming to BlogEngine 2.0? Is there some kind of rules for proposals and etc?

Aug 4, 2010 at 10:06 PM
Hi Ben ,
regarding this thread reply  could you help me how to exclude News category from front page ? Thanks in advance.


BenAmada wrote:


If you want to display the News category posts on the homepage, you can do that by modifying default.aspx.cs (in the root folder).  In there now, you'll find this line of code (I added a line break for the purposes of posting here):

 

PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
	delegate(Post p) { return p as IPublishable; }));

 

Replace that with:

 

Category newsCategory = Category.Categories.Find(delegate(Category c)
	{ return c.Title.Equals("News", StringComparison.OrdinalIgnoreCase); });
if (newsCategory != null)
	PostList1.Posts = newsCategory.Posts.ConvertAll(new Converter<Post, IPublishable>(
		delegate(Post p) { return p as IPublishable; }));
else
	PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
		delegate(Post p) { return p as IPublishable; }));

 

 

Coordinator
Aug 5, 2010 at 7:18 AM

@jha_jee, what you have now in default.aspx.cs is:

PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
	delegate(Post p) { return p as IPublishable; }));

You would want to replace that with the following to exclude Posts which are assigned to a News category:

PostList1.Posts = Post.Posts
	.FindAll(delegate(Post p)
	{
		return
			p.Categories.Find(delegate(Category c)
			{
				return c.Title.Equals("news", StringComparison.OrdinalIgnoreCase);
			}) == null;
	})
	.ConvertAll(new Converter<Post, IPublishable>(
	delegate(Post p) { return p as IPublishable; }));

Aug 5, 2010 at 12:50 PM
Thanks alot Ben ,  that worked like a charm ! You people just rock ! 

Long Live BE .

BenAmada wrote:

@jha_jee, what you have now in default.aspx.cs is:

PostList1.Posts = Post.Posts.ConvertAll(new Converter<Post, IPublishable>(
	delegate(Post p) { return p as IPublishable; }));

You would want to replace that with the following to exclude Posts which are assigned to a News category:

 

PostList1.Posts = Post.Posts
	.FindAll(delegate(Post p)
	{
		return
			p.Categories.Find(delegate(Category c)
			{
				return c.Title.Equals("news", StringComparison.OrdinalIgnoreCase);
			}) == null;
	})
	.ConvertAll(new Converter<Post, IPublishable>(
	delegate(Post p) { return p as IPublishable; }));