Copied to clipboard

Flag this post as spam?

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


  • Michaël Vanbrabandt 863 posts 3343 karma points c-trib
    Oct 31, 2014 @ 08:05
    Michaël Vanbrabandt
    0

    Access different node then Currentpage in Macro

    Hi,

    I want to create a block Recent projects that is on my Home page which displays the last 2 published projects I have added.

    I have created a node Homepage which is the root of my project, and under need a node called Projects. This contains 5 project items, 3 of them are published the others not.

    On developer > Partial Views, I have created a new view called LatestProjects which I then have added on the Homepage template. I used the snippet with the Orderby on createDate.

    Now, the CurrentPage variable in the foreach is now the Homepage, but how can I access the node Projects to collect the child items?

    Grt Mich

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Oct 31, 2014 @ 08:41
    Dennis Aaen
    0

    Hi Mich,

    With this code you should be able to list your projects on your home page.

    @{ 
        var projects = Umbraco.ContentAtRoot().DescendantsOrSelf("umbNewsOverview").FirstOrDefault();
    }
    <ul>
        @* OrderBy() takes the property to sort by and optionally order desc/asc *@
       
        @foreach (var page in projects.Children.Where("Visible").OrderBy("CreateDate desc").Take(2))
        {
            <li><a href="@page.Url">@page.Name</a></li>
        }
    </ul>

    Remember to change "Projects" in the DescendantsOfSelf in the this line:

    var projects = Umbraco.ContentAtRoot().DescendantsOrSelf("Projects").FirstOrDefault();

    So it match your document type alias of the overview page, where you have your project items under.

    I have also implemented that it only takes two of the children by using Take(2) if you change it to 3 it will display 3.

    Hope this helps,

    /Dennis

  • Michaël Vanbrabandt 863 posts 3343 karma points c-trib
    Oct 31, 2014 @ 08:57
    Michaël Vanbrabandt
    0

    Hi Dennis,

    Thanks for the reply!

    In your code you use Umbraco.ContentAtRoot() does this returns the Homepage node, eg. the root?

    Michael

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Oct 31, 2014 @ 09:07
    Dennis Aaen
    100

    Hi Michael,

    Yes it returns the root, you can read about the UmbracoHelper here including the ContentAtRoot(): http://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#ContentAtRoot%28%29

    There are also a typed version of this, if you are using strongly typed version of Razor. http://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/#TypedContent%28intid%29

    Hope this helps,

    /Dennis

  • 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