Copied to clipboard

Flag this post as spam?

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


  • Matt 353 posts 825 karma points
    Sep 19, 2019 @ 11:03
    Matt
    0

    True/False featured item

    Hello all,

    I've been using Umbraco for a few months now so I'm starting to know my way around.

    I've created a news page which works fine, then created a partial view to show the latest 4 items and everything is working fine. What I want to do is now get a little fancy....

    On my news Item documentType I've created a true/false, what I want to do is if this is set to true, then have this as a featured post which shows at the top of the website as a highlighted/important information. if nothing is set to true then just display the latest.

    I was just wondering if someone could point me in the right direction of where best to get started?

    Thanks

    Matt

  • Jason Espin 368 posts 1335 karma points
    Sep 19, 2019 @ 11:57
    Jason Espin
    0

    Hi Matt,

    Welcome to the world of Umbraco! I've been away from the forums for a while and haven't look at 8 in depth recently but this was very easy to achieve in 7 and I doubt things have changed that much.

    The first step as you have identified is to add a property to your news item documentType that indicates if the post is featured or not.

    The next step would be to create your component that is displayed at the top of the page. Notice I used the word component so I am assuming your would like this to be reusable in other areas of the site or be flexible enough to place where you want.

    Take a look at macro partials in the documentation. What you would need to do is construct a query look for the News page and then grab its children with a filter that orders by featured then by date created / updated. This way, if an item is featured it will appear at the top of the list otherwise the filter will just have the newest news item at the top. All you need to do then is select the first item in the resulting collection and return it in whatever way seems fit.

    That's the logical approach that is required. If you are still struggling later I can try and put together some example code for you this evening once I've finished work.

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 19, 2019 @ 12:23
    Shaishav Karnani from digitallymedia.com
    0

    Hi Matt,

    This query will help you.

    // ParentID of News is 1100

    var list = Umbraco.TypedContent(1100).DescendentsOrSelf

    It will give you list first ordered by Featured items and then by created date is descending order.

    Hope that helps you to solve the issue.

    Regards,

    Shaishav

  • Rakesh Kumar 22 posts 167 karma points
    Sep 19, 2019 @ 12:40
    Rakesh Kumar
    0

    Hey Matt,

    You should try this code

    var NewsNode = Model.AncestorOrSelf(1).FirstChild<News>() as News;
    
    var result = NewsNode.OrderByDescending(x => x.Value(True/False aliasName));
    
  • Matt 353 posts 825 karma points
    Sep 19, 2019 @ 12:55
    Matt
    0

    Hi,

    Thanks all for your reply.

    So this is my code which I have which shows in the features area, currently it just displays the latest and only shows 1. So I'm guessing I want to know how can I show the newsItem which is featured as true? and if nothing is true then it just shows the latest?

        var selection = Umbraco.Content(Guid.Parse("86acfb75-8edc-4d6d-933d-d8c8f555c57a"))
        .ChildrenOfType("newsItem")
        .OrderByDescending(x => x.CreateDate)
        .Take(1);
    }
    
                  @foreach (var item in selection)
                  {
                      var newsImage = item.Value<IPublishedContent>("newsImage");
                      string mainImageUrl = Model.Value<IPublishedContent>("mainImage")?.GetCropUrl(800, 450) ?? "";
    
    
                      <div class="features-div">
                          <div class="featured-image-div">
    
                              @if (newsImage != null)
                              {
                                  <img src="@newsImage.GetCropUrl("Matt")" alt="" title="" width="800">
                              }
    
                          </div>
                          <div class="features-title-div">
                              <h2 class="heading-4"><a href="@item.Url">@item.Value("newsTitle")</a></h2>
                          </div>
                          <div class="features-date">@item.CreateDate.ToString("dd MMMM yyyy")</div>
                          <p class="paragraph">@item.Value("newsTeaser")</p>
                      </div>
                  }
    
  • Jason Espin 368 posts 1335 karma points
    Sep 19, 2019 @ 13:01
    Jason Espin
    1

    It depends. A boolean true/false should evaluate to 1 or 0 so you could add an additional filter along the lines of:

    var selection = Umbraco.Content(Guid.Parse("86acfb75-8edc-4d6d-933d-d8c8f555c57a"))
        .ChildrenOfType("newsItem")
        .OrderByDescending(x => x.Featured)
        .ThenBy(x => x.CreateDate)
        .Take(1);
    

    You'll have to double check around the syntax but basically it will order by featured values that are 1 at the top then order by the most recently created. If you have no featured items everything should be 0 in which case it will just filter by the latest date.

  • Matt 353 posts 825 karma points
    Sep 19, 2019 @ 21:10
    Matt
    0

    Hi,

    I would appreciate if someone could show me some code which I would be able to use and hopefully tweak where possible.

    I'm not a great coder, still learner as I go :)

    Thanks

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Sep 19, 2019 @ 22:49
    Nik
    0

    Hi Matt,

    How are you building your site? Are you using visual studio or are you building it all from within Umbraco?

    Thanks,

    Nik

  • Matt 353 posts 825 karma points
    Sep 20, 2019 @ 07:27
    Matt
    0

    Hi Nik,

    A bit of both really I guess, I created all the templates and partial views in umbraco. but use visual studio to edit any code.

    Thanks

  • AddWeb Solution Pvt. Ltd 109 posts 360 karma points
    Sep 26, 2019 @ 06:49
    AddWeb Solution Pvt. Ltd
    0

    Hi Matt,

    you can have a look at this link https://youtu.be/mLY7asEoQak

  • Matt 353 posts 825 karma points
    Oct 08, 2019 @ 10:16
    Matt
    0

    Hi,

    Anybody able to share anything with this?

    I tried the below but doesn't seem to work;

    var selection = Umbraco.Content(Guid.Parse("86acfb75-8edc-4d6d-933d-d8c8f555c57a"))
        .ChildrenOfType("newsItem")
        .OrderByDescending(x => x.Spotlight)
        .ThenBy(x => x.CreateDate)
        .Take(1);
    
Please Sign in or register to post replies

Write your reply to:

Draft