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
I have setup a page to display news article and I can't seem to retrieve the url of the Media for the new article to add it to the SRC of the img.
I am not sure of the approach to get the value of headerImage url,.
Here is the code:
@{ var selection = Umbraco.Content(Guid.Parse("d84ed744-7582-4507-83f9-18b98fe83838")) .Children("articlesItem") .Where(x => x.IsVisible()) .OrderByDescending(x => x.CreateDate);} @foreach (var item in selection) { <div id="fh5co-blog"> <div class="container"> <div class="col-lg-4 col-md-4"> <div class="animate-box"> <a href="@item.Url"><img class="img-responsive" src="@item.Value("headerImage")" alt=""></a> <div class="blog-text"> <h3><a href="@item.Url">@item.Name</a></h3> <span class="posted_on">@item.CreateDate</span> <p>@Html.Raw(@item.Value("articleContent").ToString().Truncate(100))</p> <a href="@item.Url" class="btn btn-primary">Read More</a> </div> </div> </div> </div> </div>
Hi Pierre,
This code @item.Value("headerImage") gives you the complete media node instead of the actual url. Also, as Rann suggested, you should check if the media item and the url exists (Null/Empty check) Try using this codesnippet:
@item.Value("headerImage")
@if (item.HeaderImage != null && !String.IsNullOrEmpty(item.HeaderImage.Url)) { <a href="@item.Url"><img class="img-responsive" src="@item.HeaderImage.Url" alt=""></a> }
-Jonathan
Don't forget to check if it's not null/empty, else you'll get an exception when directly checking the .Url
.Url
Thanks Rann! I updated my answer
Thanks for your reply
Gave that a try as you suggested and in visual studio as well, it's not recognizing headerImage or HeaderImage.
here is a screenshot of Visual studio and umbraco
Alright, so youre item is dynamic. In that case, you can do it like this:
@{ var headerImage = item.Value<IPublishedContent>("headerImage"); } @if (headerImage != null && !String.IsNullOrEmpty(headerImage.Url)) { <a href="@item.Url"><img class="img-responsive" src="@headerImage.Url" alt=""></a> }
that did the trick thanks a lot for your help. much appreciated
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Umbraco CMS 8 Retrieve Media File
I have setup a page to display news article and I can't seem to retrieve the url of the Media for the new article to add it to the SRC of the img.
I am not sure of the approach to get the value of headerImage url,.
Here is the code:
}
Hi Pierre,
This code
@item.Value("headerImage")
gives you the complete media node instead of the actual url. Also, as Rann suggested, you should check if the media item and the url exists (Null/Empty check) Try using this codesnippet:-Jonathan
Don't forget to check if it's not null/empty, else you'll get an exception when directly checking the
.Url
Thanks Rann! I updated my answer
Thanks for your reply
Gave that a try as you suggested and in visual studio as well, it's not recognizing headerImage or HeaderImage.
here is a screenshot of Visual studio and umbraco
Alright, so youre item is dynamic. In that case, you can do it like this:
that did the trick thanks a lot for your help. much appreciated
is working on a reply...