Copied to clipboard

Flag this post as spam?

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


  • Hayden Tomas 11 posts 82 karma points
    Feb 05, 2019 @ 09:25
    Hayden Tomas
    0

    Pushing featured image into partial view macro file for article

    Hi

    I am trying to pull in a featured image for each article I write into a partial view macro file which will be visible on the front end.

    I have created a partial view macro file which pulls in the content of children from a specific node as follows

    <em>I wanted to add the code using the add code feature but the formatting kept breaking</em>

    In my article item document type, I have an upload property named "featuredImage"

    I would like to pulls that into the macro so it displays along with the article so that on the front end I have the image along with the truncated and html stripped content.

    Does anybody know an easy way to do this? (the featuredImage tag in the code is the bit that does not work yet and is part of my work in progress!)

    Thanks

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Feb 05, 2019 @ 10:26
    Alex Skrypnyk
    0

    Hi

    Can you please insert code as a code snippet, not an image.

    Alex

  • Hayden Tomas 11 posts 82 karma points
    Feb 05, 2019 @ 10:38
    Hayden Tomas
    0

    I did try but it kept breaking, so here goes!

    @{
    var page = Umbraco.Content(1065);
    

    }

    @page.PropertyAlias

    @foreach (var child in page.Children) {

    @Umbraco.Field("featuredImage")

    @child.Name

    @Umbraco.StripHtml(@Umbraco.Truncate(@child.ArticleContents,140))

                                            <ul class="actions">
                                                <li><a href="@child.Url" class="button">More</a></li>
                                            </ul>
                                        </article>
    

    }

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Feb 05, 2019 @ 13:00
    Alex Skrypnyk
    0

    @{ var page = Umbraco.TypedContent(1065); }

    @foreach (var child in page.Children)
    {
        @child.Name
        @Umbraco.StripHtml(@Umbraco.Truncate(@child.GetPropertyValue<string>("articleContents"), 140))
        <article>
            @if (child.HasValue("featuredImage"))
            {
                <img src="@(child.GetPropertyValue<IPublishedContent>("featuredImage").Url)"/>
            }
            <ul class="actions">
                <li><a href="@child.Url" class="button">More</a></li>
            </ul>
        </article>
    }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Feb 05, 2019 @ 13:01
    Alex Skrypnyk
    0

    Try this code, I moved the code from dynamic type to strongly typed:

     var page = Umbraco.TypedContent(1065);
    

    Image rendering:

     @if (child.HasValue("featuredImage"))
            {
                <img src="@(child.GetPropertyValue<IPublishedContent>("featuredImage").Url)"/>
            }
    
  • Hayden Tomas 11 posts 82 karma points
    Feb 05, 2019 @ 13:30
    Hayden Tomas
    0

    I got an error that it failed to load the partial view

    Error loading Partial View script (file: ~/Views/MacroPartials/homelist.cshtml)

    the homelist is my macro partial where I amended the code.

    I am not working in VS, just changing teh templates etc and making adjustments on the server, is there anything else i may have missed. I am quite new to this!

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Feb 05, 2019 @ 14:05
    Alex Skrypnyk
    0

    What version of Umbraco are you using?

  • Hayden Tomas 11 posts 82 karma points
    Feb 05, 2019 @ 14:26
    Hayden Tomas
    0

    The latest version of 7. Downloaded and installed only yesterday :)

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Feb 05, 2019 @ 21:20
    Alex Skrypnyk
    0

    Try this code:

     @if (child.HasValue("featuredImage"))
            {
                <img src="@(Umbraco.TypedMedia(child.GetPropertyValue<int>("featuredImage")).Url)"/>
            }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Feb 11, 2019 @ 16:02
    Alex Skrypnyk
    0

    Hi

    did you solve this issue?

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft