I have some news items that by default link to the news item page, but when the content picker is used to link the item to another part of the site, that URL should be used.
This is what i have:
@inherits UmbracoTemplatePage
@using System.Globalization
@{
// 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 pageTitle = string.IsNullOrWhiteSpace(CurrentPage.Title)
? CurrentPage.Name
: CurrentPage.Title;
// 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.umbNewsOverview1.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.
var newsItems = newsOverview.umbNewsItems.OrderBy("publishDate desc, CreateDate desc").Take(4);
bool containsImages = newsItems.Where("image != String.Empty").Count() > 0 ? true : false;
var featuredNews = containsImages ? (Umbraco.Web.Models.DynamicPublishedContent)newsItems.First() : null;
//var featuredNews = containsImages ? (Umbraco.Web.Models.DynamicPublishedContent)newsItems.OrderBy("image desc").First() : null;
var otherNews = newsItems;
if (containsImages && featuredNews != null)
{
var featuredNewsId = featuredNews.Id;
if(featuredNewsId > 0)
{
var values = new Dictionary<string, object>();
values.Add("excludenode", featuredNewsId);
otherNews = newsItems.Where("Id != excludenode", values);
}
}
}
<div class="col-md-6 col-sm-6">
<div class="HomeBlock2">
<div class="col-md-6 col-xs-6 npl">
<h2>Senioren</h2>
</div>
<div class="col-md-6 col-xs-6 npr">
<a href="/nieuws-senioren/">
<div class="NewsArchiveLink pull-right">
Nieuws archief
</div>
</a>
</div>
</div>
<div class="HomeBlockContent">
<div class="rowpad col-md-12">
<ul>
@if (containsImages && featuredNews != null)
{
var featuredNewsTitle = string.IsNullOrWhiteSpace(featuredNews.GetPropertyValue("title").ToString()) ? featuredNews.Name : featuredNews.GetPropertyValue("title").ToString();
var featuredNewsText = featuredNews.GetPropertyValue("bodyText").ToString();
var featuredNewsImage = featuredNews.GetPropertyValue("image");
var featuredNewsTextTruncated = Umbraco.Truncate(featuredNewsText, 100, true, false);
if(featuredNews.HasValue("linkNaarOnderdeel")){
var featuredNewsLink = Umbraco.Content(featuredNews.GetPropertyValue("linkNaarOnderdeel"));
}
else{
var featuredNewsLink = Umbraco.Content("linkNaarOnderdeel");
}
<li>
<div class="row">
<div class="imageItemHeight">
<div class="col-md-4 col-xs-4">
@if(featuredNews.HasValue("linkNaarOnderdeel")){
<a href="@featuredNewsLink">
<div class="NewsHomeImageDiv">
<img src="/ImageGen.ashx?image=@featuredNewsImage&Compression=100&width=100&height=100&constrain=true&altimage=/img/logo-vv-eemdijk.png" class="img-responsive">
</div>
</a>
}
else {
<a href="@featuredNews.Url">
<div class="NewsHomeImageDiv">
<img src="/ImageGen.ashx?image=@featuredNewsImage&Compression=100&width=100&height=100&constrain=true&altimage=/img/logo-vv-eemdijk.png" class="img-responsive">
</div>
</a>
}
</div>
<div class="col-md-8 col-xs-8 WordBreak">
<h3>
@if(featuredNews.HasValue("linkNaarOnderdeel")){
<a href="featuredNewsLink.Url">@Umbraco.Truncate(featuredNewsTitle, 57, true, false)</a>
}
else {
<a href="@featuredNews.Url">@Umbraco.Truncate(featuredNewsTitle, 57, true, false)</a>
}
</h3>
@Umbraco.StripHtml(featuredNewsTextTruncated)
</div>
</div>
</div>
</li>
}
<div class="newsMargin">
@foreach (var newsItem in otherNews)
{
// 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;
<div class="row npl npr">
<div class="HomeNewsItem">
<li>
<article class="is-post-summary WordBreak">
<div class="rowpad col-md-12">
<h3>
<img src="../Css/VVEemdijk/images/ItemImage.png" height="7" width="4" class="HomeNewsItemArrow">
@if(newsItem.HasValue("linkNaarOnderdeel")){
var onderdeelLink = Umbraco.Content(newsItem.linkNaarOnderdeel);
<a href="@onderdeelLink.Url">@Umbraco.Truncate(title, 41, true, false)</a>
}
else {
<a href="@newsItem.Url">@Umbraco.Truncate(title, 41, true, false)</a>
}
<div class="NewsItemDate">@dateTime.ToString("f", CultureInfo.CreateSpecificCulture("nl"))</div>
</h3>
</div>
</article>
</li>
</div>
</div>
}
</div>
</ul>
</div>
</div>
But i get this error when i use the content picker, when it's empty it does the right thing (that is, using the default url):
Compiler Error Message: CS0103: The name 'featuredNewsLink' does not exist in the current context
Content picker Url
Hi,
I have some news items that by default link to the news item page, but when the content picker is used to link the item to another part of the site, that URL should be used.
This is what i have:
@using System.Globalization @{
}
But i get this error when i use the content picker, when it's empty it does the right thing (that is, using the default url): Compiler Error Message: CS0103: The name 'featuredNewsLink' does not exist in the current context
is working on a reply...