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 81 karma points
    18 days ago
    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

    *I wanted to add the code using the add code feature but the formatting kept breaking*

    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 4982 posts 18113 karma points MVP 2x admin c-trib
    18 days ago
    Alex Skrypnyk
    0

    Hi

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

    Alex

  • Hayden Tomas 11 posts 81 karma points
    18 days ago
    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 4982 posts 18113 karma points MVP 2x admin c-trib
    18 days ago
    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 4982 posts 18113 karma points MVP 2x admin c-trib
    18 days ago
    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 81 karma points
    18 days ago
    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 4982 posts 18113 karma points MVP 2x admin c-trib
    18 days ago
    Alex Skrypnyk
    0

    What version of Umbraco are you using?

  • Hayden Tomas 11 posts 81 karma points
    17 days ago
    Hayden Tomas
    0

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

  • Alex Skrypnyk 4982 posts 18113 karma points MVP 2x admin c-trib
    17 days ago
    Alex Skrypnyk
    0

    Try this code:

     @if (child.HasValue("featuredImage"))
            {
                <img src="@(Umbraco.TypedMedia(child.GetPropertyValue<int>("featuredImage")).Url)"/>
            }
    
  • Alex Skrypnyk 4982 posts 18113 karma points MVP 2x admin c-trib
    1 week ago
    Alex Skrypnyk
    0

    Hi

    did you solve this issue?

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft