Copied to clipboard

Flag this post as spam?

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


  • David 29 posts 200 karma points
    Feb 22, 2019 @ 16:18
    David
    0

    Pulling Nested Content from a child node and displaying on homepage

    I have set up a "nested content" component on my "Testimonials" page which is a few levels deep within my site. my stucture is:

    Home, About > Why Us > Testimonials

    I would like to display these testimonials on my homepage. The only difference is I will want to display the top 4 testimonials on the homepage within a carousel.

    Is this possible to get these values from a child element and display on my homepage? Completely new to Umbraco and no idea where to start.

    Here is my code currently on my testimonials page.

      @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
      @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
      <div class="client-testimonials-container">
                    @{
                        var items = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("clientTestimonials");
    
                        foreach (var item in items)
                        {
                            <div class="client-testimonial">
                                @Umbraco.Field(item, "clientTestimonial")
                                <small>@item.GetPropertyValue("clientName")</small>
                            </div>
                        }
    
                    }
      </div>
    

    Any help/advice on the above would be much appreciated .

  • Marcio Goularte 374 posts 1346 karma points
    Feb 22, 2019 @ 20:56
    Marcio Goularte
    0

    try this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
     @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
    var  whyUsPage = Umbraco.TypedContentSingleAtXPath("//WhyUsAliasPage");
    
    }
      <div class="client-testimonials-container">
                    @{
                        var items = whyUsPage.GetPropertyValue<IEnumerable<IPublishedContent>>("clientTestimonials").Take(4);
    
                        foreach (var item in items)
                        {
                            <div class="client-testimonial">
                                @Umbraco.Field(item, "clientTestimonial")
                                <small>@item.GetPropertyValue("clientName")</small>
                            </div>
                        }
    
                    }
      </div>
    
  • David 29 posts 200 karma points
    Feb 26, 2019 @ 14:53
    David
    100

    Thanks for the help with this Marcio. I had to adjust how you were getting the page to pull from. I thought id rather get the page by id rather than path name incase the name of the page was changed.

    This is my working code:

                    @{
                        var testimonials = Umbraco.TypedContent(1078).GetPropertyValue<IEnumerable<IPublishedContent>>("clientTestimonials").Take(4);
    
                        foreach (var item in testimonials)
                        {
                            <div class="item">
                                <p>@Umbraco.Field(item, "clientTestimonial")</p>
                                <p class="text-right">@item.GetPropertyValue("clientName")</p>
                            </div>
                        }
    
                    }
    
  • B. Gunnarsson (Bryns) 25 posts 180 karma points c-trib
    Feb 26, 2019 @ 16:24
    B. Gunnarsson (Bryns)
    0

    Hi David, good that you found a solution. I have a second solution for you that will not fail if the page id changes.

    I'm only demonstrating this for those who will find this useful, use it if you want to.

    1. Create a Document Type named "Testimonials"
    2. Create that node under your tree and create the nested content property editor in there.
    3. Add your content.
    4. Fetch it like this: (not tested, might need a slight change from you)

    @{

    var root = Model.Content.Site();
    
    var testimonialNode = root.Descendants().Where(x => x.DocumentTypeAlias == "testimonials").FirstOrDefault();
    
    if (testimonialNode != null)
    {
        var testimonials = testimonialNode.GetPropertyValue<IEnumerable<IPublishedContent>>("clientTestimonials").Take(4);
    
        foreach (var item in testimonials)
        {
    
            <div class="item">
                <p>@Umbraco.Field(item, "clientTestimonial")</p>
                <p class="text-right">@(item.GetPropertyValue<string>("clientName"))</p>
            </div>
        }
    }
    

    }

    Happy coding!

    -B.

  • B. Gunnarsson (Bryns) 25 posts 180 karma points c-trib
    Feb 26, 2019 @ 18:27
    B. Gunnarsson (Bryns)
    0

    It was pointed out to me that .Descendants might be a bit too excessive if you have a big tree with a lot of nodes.

    If you know its under your root node, then use .Children instead.

    -B.

Please Sign in or register to post replies

Write your reply to:

Draft