Post Types

Last Updated: 2018-10-09

For general information on the different rules & components available when defining content types, please refer to How To Setup Content.

Post types are the content templates for your posts. Remember that this does not necessarily mean that all posts of the same Post type has to be rendered the same way, it simply means that they have the same content structure.

Defining a Post Type

Here's a simple example of a post with a single `MarkdownField` as content.

using Piranha.AttributeBuilder;
using Piranha.Extend.Fields;
using Piranha.Models;

[PostType(Title = "Blog Post")]
public class BlogPost : Post<BlogPost>
{
[Region(Title = "Main Content")]
public MarkdownField Body { get; set; }
}

To import this post type during your application startup you add the following line to your Configure method.

using Piranha.AttributeBuilder;

var builder = new PostTypeBuilder(api)
.AddType(typeof(BlogPost));
builder.Build();

As you can see the PostTypeAttribute allows you to set the title that will be displayed in the manager interface. If this property is left blank the class name is used.

Disable Blocks For Post Types

If you don't want to use the Block Editor you can disable this for your Post Type by adding the following to the Attribute.

[PostType(Title = "Blob Post", UseBlocks = false)]

Post Routing

By default, all post request are rewritten to the route /post. Since you want to load different model types for your posts, and often render them by different views you need to specify which route should handle your Post type. Let's say we have a post that displays an image gallery.

[PostType(Title = "Image Post")]
[PostTypeRoute(Title = "Default", Route = "/imagepost")]
public class ImagePost : Post<ImagePost>
{
[Region(ListExpand = false)]
public IList<ImageField> Images { get; set; }

public ImagePost()
{
Images = new List<ImageField>();
}
}

As you can see, requests for posts of this page type will now be routed to /imagepost.

Specifying Multiple Post Routes

You can also add multiple routes for your post types by adding several route attributes to the same class. By adding several routes the posts settings will now show a dropdown where the editor can select which route the current post should use.

[PostType(Title = "Image Post")]
[PostTypeRoute(Title = "Default", Route = "/imagepost")]
[PostTypeRoute(Title = "Gallery", Route = "/gallerypost")]
public class ImagePost : Post<ImagePost>
{
...
}