I want to render blog posts of a specific category or tag inside a standard text page of my web site, so it should not be accessed through the default Articulate /tags/ or /categories URLs but directly embedded.
My idea is to use @Html.Partial("List.cshtml", ...), but I am stucked with getting the Model for passing to the partial.
I need something very much like this where I can access the last three items (or whatever arbitrary number) on the home page of the site.
So on the blog there are these four entries:
My dog caught a ball. May 17, 2016
My cat caught a mouse. May 15, 2016
The rat was bothering the cat. May 14, 2016
The trees were green. May 12, 2015
On my home page I want to have the first three on my home page.
@{
var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
var blogArchive = blogRoot.Children.First();
foreach(var blogposts in blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")){
<p>@blogposts.Name</p>
}
}
And I added it to my home page and I now get the Name of each blog post in the archive listed on my home page. However I want the content of the post not the name.
Question:
What do I need to replace @blogposts.Name with to get the body of the blog post?
How can I get a list of the keys in blogspots so I can pick and choose what things in the post to display?
I finally found out how to get the key in the object by looking in the cmsContentXml table in the Umbraco.sdf.
In the XML column you can find the content and then use the tags there to get the names to put in blogposts.Name
To get the article content from the richText editor, you use blogposts.richText and they are then available on any page you need them with the following code.
@{
var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
var blogArchive = blogRoot.Children.First();
foreach(var blogposts in blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")){
<p>@blogposts.richText</p>
}
}
Thanks for your reply...yeah I tried that but nothing is returned. If I bring back all posts and just display the tags property (@blogposts.tags) in the page I can see the tag I'm trying to select on is in the list though!
OK More questions. How do I limit the return to only two or three items?
Right now the blog items are coming into my page with the code above from Mark in the order of oldest to newest. How can I change that to newest to oldest.
My goal is to display only the most recent two blog entries in my page. I am successfully getting them all but in reverse order.
Currently:
My first blog entry - Monday, may 1
My second blog entry - Tuesday may 2
My third blog entry - Wednesday may 3
My fourth blog entry - Thursday may 4
I want (limit 2):
My fourth blog entry - Thursday may 4
My third blog entry - Wednesday may 3
I want (limit 3):
My fourth blog entry - Thursday may 4
My third blog entry - Wednesday may 3
My second blog entry - Tuesday may 2
To sort it to news to old you could use the method .OrderBy("desc") and for limit the numbers of items that you get out, you could use the method Take(int)
So the code should look something like this.
var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
var blogArchive = blogRoot.Children.First();
foreach (var blogposts in blogArchive.Children.Where("Visible").Where("tags.Contains(@0)", "wolf").OrderBy("publishedDate desc").Take(3))
{
<p>@blogposts.tags</p>
}
Embed posts by category or tag to another page
Hi,
I want to render blog posts of a specific category or tag inside a standard text page of my web site, so it should not be accessed through the default Articulate /tags/ or /categories URLs but directly embedded.
My idea is to use @Html.Partial("List.cshtml", ...), but I am stucked with getting the Model for passing to the partial.
Any hints?
Best Regards Andreas
I need something very much like this where I can access the last three items (or whatever arbitrary number) on the home page of the site. So on the blog there are these four entries:
On my home page I want to have the first three on my home page.
OK. More progress.
I found this code:
And I added it to my home page and I now get the Name of each blog post in the archive listed on my home page. However I want the content of the post not the name.
Question:
What do I need to replace @blogposts.Name with to get the body of the blog post?
How can I get a list of the keys in blogspots so I can pick and choose what things in the post to display?
OK Yet more progress...
I finally found out how to get the key in the object by looking in the cmsContentXml table in the Umbraco.sdf.
In the XML column you can find the content and then use the tags there to get the names to put in blogposts.Name
To get the article content from the richText editor, you use blogposts.richText and they are then available on any page you need them with the following code.
Hi Matt,
Each of your fields on your document type has an alias, and it´s by this alias that you can get data from a field in Umbraco.
So in your case the field for the article content has a alias of richText.
Try to see this video, that describes the concepts https://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/document-types/tabs-and-properties/
Hope this helps,
/Dennis
Hi Matt,
thanks for the input! I will try that.
Best Regards Andreas
Please ping me if it doesn't work. I am more than excited to share what I found if it helps you.
Hi Matt,
Did you figure out how to get all bog posts by Tag?
Cheers Mark
Yes. You change the foreach Where("categories== @0","testCategory") to .Where("tags == @0","wolf")
Your code would look like this:
Hi Matt,
Thanks for your reply...yeah I tried that but nothing is returned. If I bring back all posts and just display the tags property (@blogposts.tags) in the page I can see the tag I'm trying to select on is in the list though!
aah...dynamic linq...do I need to include a namespace?
Got it...thanks for your help...I have multiple tags per post! So this works for me...
COOL! Thank you. I am will update my code as well. This has been a good interaction. Thank you!
OK More questions. How do I limit the return to only two or three items?
Right now the blog items are coming into my page with the code above from Mark in the order of oldest to newest. How can I change that to newest to oldest.
My goal is to display only the most recent two blog entries in my page. I am successfully getting them all but in reverse order.
Hi Matt,
To sort it to news to old you could use the method .OrderBy("desc") and for limit the numbers of items that you get out, you could use the method Take(int)
So the code should look something like this.
Hope this helps,
/Dennis
OK that is a big help and Take(x) works great. However the OrderBy("desc") gives an error of System.InvalidOperationException:
The solution is to add the field that you want to sort on in the OrderBy() so that would be OrderBy("publishedDate desc")
So working code for me looks like this:
Thanks again for the help on this.
is working on a reply...