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 96 posts 341 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 1413 posts 6212 karma points MVP 3x 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 96 posts 341 karma points
    Jan 07, 2020 @ 09:39
    Simeon Ostberg
    1

    Hi Nik,

    That did the trick!

    Thanks a million!!

    Best, Simeon

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies