Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
So I've been following this tutorial on umbraco;
And I'm now looking to show the latest blog posts on the home page and was wondering if someone can point me in the right direction of how I could achieve this.
This is what I'm looking to do;
Thanks in advance,
Assuming the Homepage is your root node, something like the following should do it Model.Content.Descendants("blog").FirstOrDefault(), where blog is your document type alias
If you're using models builder you can do Descendants<Blog>() instead
is it possible to show us a preview of your content structure?
Presume you have a structure like this:
Blog ( document type `Blog Root` )
Blogpost 1 ( document type `Blog Item` )
Blogpost 2 ( document type `Blog Item` )
Blogpost 3 ( document type `Blog Item` )
Then you can do something like this:
First we are going to take the Blog node
var blogNode = UmbracoHelper.TypedContentSingleAtXPath("//blogRoot");
So basically we ask Umbraco to take the first node from top which has a document type alias of blogRoot.
Get latest Blog Item nodes
var blogItems = blogNode.Children.OrderByDescending(x => x.CreateDate).Take(3);
Here we ask for the children of the Blog Root node and sort them by there creation date, then at the end using Take() we can say to only return the last 3 items.
*Code is manually written, so could contain typos.
Hope this helps.
I could be wrong here, it's been a while since I checked, but shouldn't it be
OrderByDescending(c => c.CreatedDate) so it doesn't throw deprecation warnings for dynamic casting?
OrderByDescending(c => c.CreatedDate)
Yes you are correct, I have edit the post above!
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted