Add a Blog Post Publication Date
In Part Two, we displayed a nicely formatted publication date on the blog post page.
Finally, in Part Three we shall change the blog listing.
Steps - Part Three
In the Settings section, expand the Partial Views > Components > LatestBlogPosts folder.
Select Default.cshtml.
Scroll down to find the
foreach
loop.Declare a new
publicationDate
variable as the first thing within the loop:Locate the
blogpost-date
element a bit further down and change it to use the new variable:The ToLongDateString() method is called on the
publicationDate
variable to format it as a long date string.
Redefine the
blogposts
variable before the firstdiv
tag - this will be used for sorting the posts:Because we are sorting by a custom property we need to use the generic
Value
method.
You can use Query builder to construct your queries in a more structured and reusable manner. Use the UmbracoHelper
or the IPublishedContentQuery
interface to build queries dynamically. For more information, see the Querying & Models article.
Locate the
@foreach
loop, and changeModel.Blostposts
to the variable created above:blogposts
:Save the partial view - a confirmation message should appear confirming that the Partial view has been saved.
Now view both the Blog overview and the blog posts themselves in the browser to confirm that all is working as expected.
Summary
Nice job! In this lesson, you've learned what a Document Type is and how to add a new Property to it. You've also learned how to change Templates and sort by custom Properties.
Last updated