Copied to clipboard

Flag this post as spam?

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


  • Jin Botol 134 posts 287 karma points
    Nov 25, 2017 @ 05:05
    Jin Botol
    0

    How to show related articles?

    Hi,

    I want to display a random related articles inside the articles page. Everything works well, but I want to exclude also the CurrentPage.

    For example: I'm in the Articles 1 Page and I want to display only the Articles 2, Articles 3 & Articles 4

    But in my case, the Articles 1 is also shown.

    The Website structure.

    > Homepage
      > Articles
        > Articles 1
        > Articles 2
        > Articles 3
        > Articles 4
        > [...]
    

    Here's my code;

    var max = CurrentPage.Parent.Children.Where("Visible");
    foreach (var relatedItems in CurrentPage.Parent.Children.Where("Visible").Random(3))
    {
        <h4>@relatedItems.Id => @relatedItems.Name</h4>
    }
    

    I tried;

    var max = CurrentPage.Parent.Children.Where("Visible");
    foreach (var relatedItems in CurrentPage.Parent.Children.Where("Visible").Random(3))
    {
        if (relatedItems.Name != Model.Content.Name)
        {
           <h4>@relatedItems.Id => @relatedItems.Name</h4>
        }
    }
    

    Its works, but sometimes its only display two articles. I think the third article is hidden because of the if statement.

    I'm stuck with this, any help would be appreciated!

    Thank you in advance.

    Jin

  • Matt Darby 28 posts 391 karma points c-trib
    Nov 25, 2017 @ 06:56
    Matt Darby
    1

    Hey Jin,

    You could simplify this with the use of .Siblings for example:

    @foreach(var page in Model.Content.Siblings().RandomOrder())
    {
        @page.Name
    }
    

    Check out https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets for some more tips. It says v6 but is still relevant.

  • Jin Botol 134 posts 287 karma points
    Nov 27, 2017 @ 02:17
    Jin Botol
    0

    Hi Matt,

    Thanks for your response.

    But it keeps displaying the Article 1 page.

    My question is while I'm in the Article 1 page I don't want the article 1 shows in the Related Articles

    Problem; the article 1 is showing in the Related Articles.

    Hope I explain well :(

    Regards,

    Jin

  • Jin Botol 134 posts 287 karma points
    Nov 27, 2017 @ 02:38
    Jin Botol
    0

    Hi Matt,

    Its working now, thanks to you :)

    Regards,

    Jin

  • Daniel Chenery 119 posts 465 karma points
    Nov 25, 2017 @ 09:52
    Daniel Chenery
    100

    Alongside Siblings, you should really use an ID to compare rather than a name. You can do it in a lambda clause like so.

    Model.Content.Sibings().Where(x => x.Id != Model.Content.Id)

    If you're using Models Builder you can even typecast to ensure it's only Articles you get Model.Content.Siblings<Article>()...

  • Jin Botol 134 posts 287 karma points
    Nov 27, 2017 @ 02:37
    Jin Botol
    1

    Hi Daniel,

    It's working now :)

    Thanks for your help!

    Regards,

    Jin

Please Sign in or register to post replies

Write your reply to:

Draft