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.
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:
Home
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.
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?
Show latest blogs on home page
Hello all,
So I've been following this tutorial on umbraco;
https://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/creating-a-site-from-scratch/preview-of-completed-site/
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,
Matt
Assuming the Homepage is your root node, something like the following should do it
Model.Content.Descendants("blog").FirstOrDefault()
, whereblog
is your document type aliasIf you're using models builder you can do
Descendants<Blog>()
insteadHi Matt,
is it possible to show us a preview of your content structure?
Presume you have a structure like this:
Then you can do something like this:
First we are going to take the
Blog
nodeSo basically we ask Umbraco to take the first node from top which has a document type alias of
blogRoot
.Get latest
Blog Item
nodesHere 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.
/Michaël
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?Yes you are correct, I have edit the post above!
/Michaël
Hi Matt,
did you solve this issue? Can you share it with the community for others that are in the same situation as you?
Have a nice day
/Michaël
Currently having problems with our install at the moment (not related to this)
But once its up and working again I will post back :)
Hello,
I created it using partial views and using the query builder.
My next question is how do I display the latest 3? currently its displaying them from 1st blog created.
As mentioned above,
OrderByDescending(c => c.CreatedDate)
is your friend.Try this
Hmm that didnt work for me I had to use;
Which done the job, thanks :)
Ah, sounds like you're using the older Dynamics syntax.
It works in V7, but is due to be removed in V8 - In fact, you should have deprecation warnings in the current versions of Umbraco I believe :)
I would always have a datetime field for article date. That way you can create or update an article and not have it interfere with the ordering.
is working on a reply...