Copied to clipboard

Flag this post as spam?

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


  • Leon 101 posts 489 karma points
    Mar 05, 2015 @ 18:38
    Leon
    0

    Home page BlogPostRepository

    Hello.

    I'm using Umbraco 7.2.2

    I have a list of blog posts in a blog post repository that I want to display on the front page. What is the razor syntax to do that?

    I thought one of these three code snippets would work, but not having any joy. Please help:

    var blogPosts = CurrentPage.Site().Children("BlogPostRepository");
    var blogPosts = CurrentPage.Site().Children("BlogPostRepository").Children("BlogPost");
    var blogPosts = CurrentPage.Site().Children("BlogPostRepository")[0].Children("BlogPost");
    

    Any help would be most welcome.

    Leon

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Mar 05, 2015 @ 20:09
    Alex Skrypnyk
    1

    Hi Leon,

    Try to use Descendants instead of Children, like that :

    Umbraco.TypedContentAtRoot().DescendantsOrSelf("BlogPostRepository")
    

    Thanks

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 05, 2015 @ 20:10
    Jan Skovgaard
    0

    Hi Leon

    You can do it like this

    @{
        var blogPosts = CurrentPage.Descendants("BlogPost");
    
       foreach(var blostPost in blogPosts){
           blogPost.Id <br />
       }
    }
    

    I think you can also benefit from reading this cheat sheet of available methods etc. https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    Hope this helps.

    /Jan

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Mar 05, 2015 @ 21:29
    Dennis Aaen
    0

    Hi Leon,

    If you are using the query builder in Umbraco 7.2.2 then you can use this Razor to display blogposts on the frontpage. You can find the query builder here.

    @{
        var selection = CurrentPage.Site().FirstChild("BlogPostRepository").Children("BlogPost").Where("Visible");
    }
    <ul>
        @foreach(var item in selection){
            <li>
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>

    Hope this helps,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft