Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simeon Ostberg 123 posts 389 karma points
    Jan 07, 2020 @ 09:17
    Simeon Ostberg
    0

    Featured image in blog overview

    Hi everyone,

    I'm trying to set up a blog, and the individual post is working already, but I don't manage to get the image into the blogoverview.

    I use a partial macro file, and this would be my code:

    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Web;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ 
         var startNodeId = Model.MacroParameters["startNodeId"] != null ? Model.MacroParameters["startNodeId"] : Model.Content.Id;
         var numberOfPosts = 3;
         if (Model.MacroParameters["numberOfPosts"] != null)
         {
              int.TryParse((string)Model.MacroParameters["numberOfPosts"], out numberOfPosts);
         }
      }
    @if (startNodeId != null)
    {
    var startNode = Umbraco.TypedContent(startNodeId);
    var blogposts = startNode.Children.OrderByDescending(x => x.CreateDate).Take(numberOfPosts);
    
    if (blogposts.Any())
    {
        <div class="blogposts">
            @foreach (ContentModels.Blogpost post in blogposts)
            {
                <a href="@post.Url">
                    <h5 class="heading-alt">@post.PageTitle</h5>
                    <div class="blogpost-meta">
                        <small class="blogpost-date">@post.CreateDate.ToShortDateString()</small>
                    </div>
                    <div class="blogpost-excerpt">@post.Excerpt</div>
                    <img src="@post.GetPropertyValue("BlogImage")" />
                </a>
            }
        </div>
    }
    

    This is the closest I could get. With this code I at least see an unloaded image with the URL "".

    I would be happy, if anyone could help me to get the image into the blogpost overview.

    Thanks, Simeon

  • Nik 1612 posts 7258 karma points MVP 7x c-trib
    Jan 07, 2020 @ 09:29
    Nik
    101

    Hi Simeon,

    Try this:

    @if(post.BlogImage != null){
    
        <img src="@post.BlogImage.Url" />
    }
    

    This should, I believe, give you your image. (Making the assumption that BlogImage property is a media picker)

    Thanks

    Nik

  • Simeon Ostberg 123 posts 389 karma points
    Jan 07, 2020 @ 09:39
    Simeon Ostberg
    1

    Hi Nik,

    That did the trick!

    Thanks a million!!

    Best, Simeon

Please Sign in or register to post replies

Write your reply to:

Draft