How to get the UmbNewsOverviewWidget to show 4 posts on homepage
I am pulling apart the TXT Starter Kit for Umbraco7 in order to try and understand it and adapt it to a potential project. I can invision me wanting the four lastest news posts to feature on my homepage design, each with a thumbnail image and a dynamic title.
I can see that the UmbNewsOverviewWidget adds 'One' news item to the homepage, but how do I get this to show the last four news items I created?
In addition, I can see that the News Overview template (Not to be confused with the widget) loops through and gets all the news stories to display on one standard text page, but how would I restrict this to show the last eight juicy stories of hot gossip and add pagination for more?
What you need to do this add this linie on your homepage template where you want it to appear
@{ Html.RenderPartial("umbLatestNewsWidget"); }
And the change the take from five to four. What this line does is that it takes the five latest news, and output it on a sorted order, by the publishDate, if it doesn't has s publishDate set, then it will fallback to create date.
var newsItems = newsOverview.umbNewsItems.OrderBy("publishDate desc, createDate desc").Take(5);
Thanks for that. I guess I was looking at it the wrong way. Latest News Widget is used to show the last 5 posts (now 4) . So these can be displayed on my homepage. The News Overview Widget isn't required in this instance and the News Overview Template is just a good ol'loop to display my news posts on a standard text page.
Where do I put the MVC Dynamic code?
@{ if (CurrentPage.HasValue("mainImage")){ var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage); } }
as adding it to the UmbLatestNewsWidget it does'nt do anything? :)
The code should look like this. And you can change the div around to what tag you like.
@inherits UmbracoTemplatePage @{ // Model.Content is the current page that we're on // AncestorsOrSelf is all of the ancestors this page has in the tree // (1) means: go up to level 1 and stop looking for more ancestors when you get there // First() gets the first ancestor found (the home page, on level 1) var homePage = CurrentPage.AncestorsOrSelf(1).First();
// Find all pages with document type alias umbNewsOverview // We do that using the plural, umbNewsOverviews (note the extra "s" in the end) // Then take the first one, as we know there will only be on news overview page var newsOverview = homePage.umbNewsOverviews.First();
// Similar to above: find all pages with document type umbNewsItem under the news overview page // Then order them, first by publishDate (a property the editor can explicitly set on the news item) // and then by createDate, which is set by Umbraco automatically when a page gets created. // Finally, take the first 5 items to show in the news overview var newsItems = newsOverview.umbNewsItems.OrderBy("publishDate desc, createDate desc").Take(4); }
<h2 class="major"><span>Recent Posts</span></h2>
<!-- Archives --> <ul class="style2"> @foreach (var newsItem in newsItems) { // If the editor has not explicitly provided the "Page title" property page // then just show the name of the page otherwise show the provided title var title = string.IsNullOrWhiteSpace(newsItem.Title) ? newsItem.Name : newsItem.Title;
// If the editor has not explicitly set the publishDate property then show the create date var dateTime = newsItem.PublishDate == default(DateTime) ? newsItem.CreateDate : newsItem.PublishDate;
Boom, the image comes through now :) I can see that the size wll need to be adjusted. However, I can do this in the CSS to make it fluid to fit my HTML layout. Amazing, thanks for yor help! :)
How to get the UmbNewsOverviewWidget to show 4 posts on homepage
I am pulling apart the TXT Starter Kit for Umbraco7 in order to try and understand it and adapt it to a potential project. I can invision me wanting the four lastest news posts to feature on my homepage design, each with a thumbnail image and a dynamic title.
https://github.com/umbraco/Starterkits/blob/master/Txt/Txt/Views/Partials/umbNewsOverviewWidget.cshtml
I can see that the UmbNewsOverviewWidget adds 'One' news item to the homepage, but how do I get this to show the last four news items I created?
In addition, I can see that the News Overview template (Not to be confused with the widget) loops through and gets all the news stories to display on one standard text page, but how would I restrict this to show the last eight juicy stories of hot gossip and add pagination for more?
Any help welcome. :)
Hi Hannah,
If you you are using the TXT Starter Kit for Umbraco 7, then there is an example on how to get the four lastest news posts to feature on your homepage. Try to see this file, https://github.com/umbraco/Starterkits/blob/master/Txt/Txt/Views/Partials/umbLatestNewsWidget.cshtml
What you need to do this add this linie on your homepage template where you want it to appear
And the change the take from five to four. What this line does is that it takes the five latest news, and output it on a sorted order, by the publishDate, if it doesn't has s publishDate set, then it will fallback to create date.
For the thumbnail image you can use the media picker https://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors/Media-Picker look the MVC View Example stongly or dynamic not the DynamicNode is old razor syntax which is deprecated or the image cropper data type. https://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors-v7/Image-Cropper
For the pagination take a look here: https://our.umbraco.org/forum/developers/razor/41166-Razor-Paging-snippet I think that you should look at Craig´s comment or Lee Kelleher
Hope this helps. If you have further questions don't hesitate to ask again,
/Dennis
Thanks for that. I guess I was looking at it the wrong way. Latest News Widget is used to show the last 5 posts (now 4) . So these can be displayed on my homepage. The News Overview Widget isn't required in this instance and the News Overview Template is just a good ol'loop to display my news posts on a standard text page.
Where do I put the MVC Dynamic code?
as adding it to the UmbLatestNewsWidget it does'nt do anything? :)
Hi Hannah,
I can see that you already have the option to add an image. It´s using the upload type , so I assume that you just want to pull out the same image as you are using on the newspage detail view. You can find the documentation here: https://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/built-in-property-editors/Upload
The code should look like this. And you can change the div around to what tag you like.
Hope this helps,
/Dennis
Boom, the image comes through now :) I can see that the size wll need to be adjusted. However, I can do this in the CSS to make it fluid to fit my HTML layout. Amazing, thanks for yor help! :)
is working on a reply...