Copied to clipboard

Flag this post as spam?

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


  • Shaun 248 posts 475 karma points
    Aug 14, 2014 @ 10:52
    Shaun
    0

    Displaying Articulate blog posts on my homepage

    Hi All

    I'm working with articulate for the first time, and I'm trying to display posts from the blog on my homepage.

    My homepage template contains the following

    @Html.Partial("/views/partials/displayblogitemsonhomepage.cshtml", new Articulate.Models.ListModel(Umbraco.Content(1368)))

    and my partial view is as follows

    using Articulate
    @using Articulate.Models
    @using Umbraco.Core
    @using Umbraco.Web
    @model Articulate.Models.ListModel
    
    @{
        Layout = null;
    }
    
    <h3>Recent News</h3>
    
    @if (!Model.Children.Any()) //getting object ref not found error here
    {
        <p>No blog posts found</p>
    }
    else
    {
        @foreach (var post in Model.Children<PostModel>())
        {
            //show posts
        }    
    }

    however it's giving me object reference not found errors. The Node Id I'm passing is the node for the archive section of the articulate blog in the backend, clearly this isn't the way to do it!

    Can anyone point me in the right direction?

    Cheers

    Shaun

     

  • Russell McGinnis 48 posts 183 karma points
    Aug 14, 2014 @ 19:52
    Russell McGinnis
    1

    Shaun, not sure if this is the way to go, however this is the approach I am taking...

    var root = CurrentPage.AncestorOrSelf(1);

    var blogHome = root.Children.Single(x => x.DocumentTypeAlias == "Articulate");

    var blogArchive = blogHome.Children.Single(x => x.DocumentTypeAlias == "ArticulateArchive");

    ListModel lModel = new ListModel(blogArchive, blogArchive.Children(),

            new PagerModel(blogArchive.Children().Count(), 1, blogArchive.Children().Count()/10));

    Not sure about the PagerModel issue, I am using the lModel variable to call:

    Umbraco.GetRecentPosts(lModel, 5)

     

    Would love to know if there is a better way of doing this.

     

    Russell

  • Shaun 248 posts 475 karma points
    Aug 15, 2014 @ 02:21
    Shaun
    100

    Cheers Russell!

    I couldn't quite get your code to work,but it did lead me to this solution. Thanks again!

    Below is my new partial view, it's being called by the same html.partial statement as the one above

    @using Articulate
    @using Articulate.Models
    @using Umbraco.Core
    @using Umbraco.Web 
    
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Articulate.Models.ListModel> 
    
    <h3>Recent News</h3>
    
    @{
        var root = Model.AncestorOrSelf(1);
        var blogRoot = root.Children;
        var blogArchive = root.Children.First();
    
        if (!blogArchive.Children.Any())
        {
            <p>No blog posts found</p>
        }
        else
        {
            string strLastClass = "";
    
            <ul class="recent_blogs">
                @foreach (var post in blogArchive.Children)
                {
                    if (post.IsLast())
                    {
                        strLastClass = "last";
                    }
                    <li class="@strLastClass">
                        <h3><a href="@post.Url">@post.Name</a></h3>
                        <p>@post.GetPropertyValue("Excerpt")</p>
                    </li>
                }
            </ul>
        }
    }
    
  • Phillip Ng 148 posts 250 karma points
    Nov 04, 2014 @ 04:08
    Phillip Ng
    0

    Anybody have luck with manipulating articleDate into a different format? I have something like

    foreach (var post in blogArchive.Children.OrderByDescending(b => b.GetPropertyValue("publishedDate")).Take(3))
        {
            <li class="@(post.IsLast() ? " last" : "" )">
                <span>@post.GetProperty("publishedDate").ToString("MMMM dd, yyyy")</span>
            </li>
        }

    but the stack trace error says "No overload for method 'ToString' takes 1 arguments." @post.CreateDate.ToString("MMMM dd, yyyy") works great. Do I need to cast or access the property a little differently? Thanks in advance!

  • Nathan Skidmore 63 posts 251 karma points c-trib
    Dec 18, 2014 @ 13:06
    Nathan Skidmore
    0

    Phillip, you are trying to apply the date formatting to an object, not a DateTime type. You will need to cast the object as a DateTime object first, before executing the method ToString("MMMM dd, yyyy). Cheers.

     

  • dave 36 posts 149 karma points
    Apr 23, 2015 @ 03:23
    dave
    0

    I followed the above notes and am getting the following exception

    The best overloaded method match for 'Articulate.Models.ListModel.ListModel(Umbraco.Core.Models.IPublishedContent)' has some invalid arguments

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Any ideas as to how to resolve? I am new to Umbraco and don't quite understand the integration between articulate and umbraco. See also this post https://our.umbraco.org/forum/umbraco-as-a-service/general-questions/64222-Umbraco-7-plus-Articulate-blog-engine

  • Shaun 248 posts 475 karma points
    Apr 23, 2015 @ 10:06
    Shaun
    0

    Hi Dave

    Could it be related to the node you're pulling back as the root for the articulate blog? I've noticed that the logical node to pull back would be the main blog node, but actually if you're pulling stuff through to the homepage you're better off using the archive node instead.

    EDIT: Removed my code as I realised it was the same stuff I posted above. 

  • dave 36 posts 149 karma points
    Apr 23, 2015 @ 14:10
    dave
    0

    I did get it working by passing in the correct id, however i did find some odd behavior.

    In my view i was using var root = Model.AncestorOrSelf(1); > When using this and stepping through the code, it would throw a null exception unless i stepped through very slow. I replaced with var root = Model.RootBlogNode; to avoid this problem

    What makes it odd is that the AncestorOrSelf approach would NOT WORK if i ran the site without debugging. Slowing it down however allowed Model.AncestorOrSelf(1) to resolve itself to something other than null.

    I can't explain this.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 11, 2015 @ 09:25
    Owain Williams
    0

    Hi, Just got this working and pulling a list of recent blog posts through to my homepage but I was wonder. Is there a way to pull 2 archives in to one feed? Just now I have a partial view and then use two Html.Partial calls on my template.

     @Html.Partial("umbLatestBlogWidget", new Articulate.Models.ListModel(Umbraco.Content(1298)))
                @Html.Partial("umbLatestBlogWidget", new Articulate.Models.ListModel(Umbraco.Content(1312)))
    

    This then gives me two separate lists.

    I guess I need to somehow read the two lists and then merge them together in date order but I have no idea where to start with that.

  • Nathan Skidmore 63 posts 251 karma points c-trib
    Jun 11, 2015 @ 10:40
    Nathan Skidmore
    0

    Hi Owain,

    You can union the two lists together, then load the partial view.

    For example, save the lists into variables first, then perform a union on the list and pass that to the partial view. The union will remove any duplicate entries.

    var list1 = new Articulate.Models.ListModel(Umbraco.Content(1298));

    var list2 = new Articulate.Models.ListModel(Umbraco.Content(1312));

    @Html.Partial("umbLatestBlogWidget", list1.Union(list2))

    Hope this helps.

    Nathan

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 11, 2015 @ 10:51
    Owain Williams
    0

    Thanks for the speedy reply Nathan! I gave your solution a go but I get an error saying

    CS0103: The name 'list1' does not exist in the current context
    

    Which is the link the @Html.Partial is on.

  • Nathan Skidmore 63 posts 251 karma points c-trib
    Jun 11, 2015 @ 11:03
    Nathan Skidmore
    0

    Hi Owian,

    You will need to use razor syntax to use the variables. Try:

    E.g.

    @{

    var list1 = new Articulate.Models.ListModel(Umbraco.Content(1298));

    var list2 = new Articulate.Models.ListModel(Umbraco.Content(1312));

    }

    @Html.Partial("umbLatestBlogWidget", list1.Union(list2))

    Cheers

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 11, 2015 @ 11:09
    Owain Williams
    0

    Of course! Stupid me. Still learning Umbraco & Razor, as if you couldn't notice :)

    The next issue is Articulate doesn't have a definition for Union. For something that should be straight forward it's causing many issues or the lack of my own understanding is probably causing a lot of issues!

    Compiler Error Message: CS1061: 'Articulate.Models.ListModel' does not contain a definition for 'Union' and no extension method 'Union' accepting a first argument of type 'Articulate.Models.ListModel' could be found (are you missing a using directive or an assembly reference?)
    
  • Nathan Skidmore 63 posts 251 karma points c-trib
    Jun 11, 2015 @ 12:57
    Nathan Skidmore
    0

    Apologies, I hadn't test the above. The ListModel cannot have union applied to it as it's not a list, it's a model that contains a list as well as other properties.

    To do the union you will need to do something like this:

    @{
    
    var list1 = new Articulate.Models.ListModel(Umbraco.Content(1298)).Children();
    
    var list2 = new Articulate.Models.ListModel(Umbraco.Content(1312)).Children();
    
    }
    
    @Html.Partial("umbLatestBlogWidget", list1.Union(list2))
    

    However now the type has changed in the partial. Therefore the model in the partial will need to be changed to:

    @model IEnumerable<IPublishedContent>
    

    If you do wish to access any of the ListModel properties within the partial, you still can by accessing the parent.

    E.g.

    @foreach (var item in Model)
    {
        var parent = item.Parent as ListModel;
    }
    
  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 11, 2015 @ 13:35
    Owain Williams
    0

    Sorry, this is just not working for me. I wish I could even troubleshoot rather than constantly relying on your good efforts.

    My master page has:

    @{
                var list1 = new Articulate.Models.ListModel(Umbraco.Content(1298)).Children(); 
                var list2 = new Articulate.Models.ListModel(Umbraco.Content(1312)).Children();
    
                }
    
    
                @Html.Partial("umbLatestBlogWidget", list1.Union(list2));
    

    My partial looks like this:

    @using Articulate
    @using Articulate.Models
    @using Umbraco.Core
    @using Umbraco.Web
    
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Articulate.Models.ListModel>
    
    
    
    @{
        var root = Model.AncestorOrSelf(3);
        var blogRoot = root.Children;
        var blogArchive = root.Children.First();
    
    
    
    
        if (!blogArchive.Children.Any())
        {
            <p>No blog posts found</p>
        }
        else
        {
            string strLastClass = "";
    
            <ul class="recent_blogs">
                @foreach (var post in blogArchive.Children.OrderByDescending(b => b.GetPropertyValue("publishedDate")).Take(3))
                {
                    if (post.IsLast())
                    {
                        strLastClass = "last";
                    }
    
    
    
                    <h4>Blogs</h4>
                    <li class="@strLastClass">
                        <h5><a href="@post.Url">@post.Name</a></h5>
                        <p>@post.GetPropertyValue("Excerpt")</p>@post.GetPropertyValue("publishedDate")
    
                    </li>
                }
            </ul>
        }
    }
    

    I tried changing the inherits to model but I still get horrible errors.

    Just now the error I get on the first var list1 = new ...... is

    System.NullReferenceException: Object reference not set to an instance of an object.

  • Nathan Skidmore 63 posts 251 karma points c-trib
    Jun 11, 2015 @ 16:15
    Nathan Skidmore
    0

    You will need to change the @inherits to accept an IEnumerable

    E.g.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<IPublishedContent>>
    

    This will break your partial because you are still trying to deal with a ListModel in the code. Therefore if you need to use ListModel properties you will need to get the parent of a list item first, then perform the rest of your code on that

    E.g.

    @{
    
    var parentItem = Model.FirstOrDefault().Parent as ListModel;
    
    var root = parentItem.AncestorOrSelf(3);
    var blogRoot = root.Children;
    var blogArchive = root.Children.First();
    
    ...
    
    }
    
  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 11, 2015 @ 21:25
    Owain Williams
    0

    Thanks! I think I've got it almost there with your help but I'm just not getting this last bit. I've changed the inherits in my partial to what you suggest, I've change the parentItem etc. But the line with

     var list1 = new Articulate.Models.ListModel(Umbraco.Content(1297)).Children();
    

    Is throwing an error due to it returning a Null.

    I've finally got it building in VisualStudio rather than trying to just to it within Umbraco and it build fine with no errors.

  • Nathan Skidmore 63 posts 251 karma points c-trib
    Jun 12, 2015 @ 08:27
    Nathan Skidmore
    0

    OK it sounds like you have made some progress.

    If list1 is null, it means that either content item 1297 doesn't exist, it is not a ListModel, or it has no children (blog posts). Ensure that all the above are satisfied and it should work.

    For error proofing, you could check for null values first before trying to get the children.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 12, 2015 @ 10:16
    Owain Williams
    0

    Content item 1297 exists because it was pulling the blog posts through when I was doing one blog, one list. It has blog posts in it so what it looks like is it's not a ListModel - how would I check / fix this if that is the case?

    Cheers.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 16, 2015 @ 07:48
    Owain Williams
    0

    I really appreciate the help but I'm still stuck trying to pull two blog streams together so that it shows them in one list on my homepage. Is anyone else able to help me please.

    Thanks in advance.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 17, 2015 @ 21:24
    Owain Williams
    0

    I've been trying to get this to work for the past couple of days, I thought I had cracked it but I'm still getting the error:

        An exception of type 'System.NullReferenceException' occurred in Articulate.dll but was not handled in user code
    
    Additional information: Object reference not set to an instance of an object.
    

    Now, if I use the number 1289 or 1312 to display the one blog, it pulls the posts with no issues so I know that this is working but I also know that I am obviously not understanding how this works from all the help I've been given so far.

    My masterpage has:

    <div class="col-md-4">
                @{
    
                    var list1 = new Articulate.Models.ListModel(Umbraco.Content(1289)).Children();
                    var list2 = new Articulate.Models.ListModel(Umbraco.Content(1312)).Children();
    
                    list1.ToList();
                    list2.ToList();
    
    
                }
    
    
                @Html.Partial("umbLatestBlogWidget", list1.Union(list2));
    
            </div>
    

    my Partial has:

    @using Articulate
    @using Articulate.Models
    @using Umbraco.Core
    @using Umbraco.Web
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IEnumerable<IPublishedContent>>
    
    @{
        var parentItem = Model.FirstOrDefault().Parent as ListModel;
    
        var root = parentItem.AncestorOrSelf(3);
        var blogRoot = root.Children;
        var blogArchive = root.Children.First();
    
    
    
    
        //var root = Model.AncestorOrSelf(3);
        //var blogRoot = root.Children;
        //var blogArchive = root.Children.First();
    
    
    
    
        if (!blogArchive.Children.Any())
        {
            <p>No blog posts found</p>
        }
        else
        {
            string strLastClass = "";
    
            <ul class="recent_blogs">
                @foreach (var post in blogArchive.Children.OrderByDescending(b => b.GetPropertyValue("publishedDate")).Take(3))
                {
                    if (post.IsLast())
                    {
                        strLastClass = "last";
                    }
    
    
    
                    <li class="@strLastClass">
                        <h5><a href="@post.Url">@post.Name</a></h5>
                        <p>@post.GetPropertyValue("Excerpt")</p>@post.GetPropertyValue("publishedDate")
    
                    </li>
                }
            </ul>
        }
    }
    

    Can anyone tell me where my mistake is please....

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Jun 18, 2015 @ 11:29
    Owain Williams
    0

    I found a checkbox within the document type for the articulate archive which wasn't ticked - "enable list view" I clicked it and hoped this would solve the issue, but nope, I still being told

    var list1 = new Articulate.Models.ListModel(Umbraco.Content(1289)).Children();
    

    is returning a null value, i've changed the node number to see if that helps but nothing.

  • Bobi 352 posts 956 karma points
    Feb 22, 2017 @ 19:10
    Bobi
    0

    Hi,

    I have a question that seems to be driving me mad. How can I pull a partial view into my home page simple? I just want to reference:

    @Html.ThemedPartial(Model, "Latest")

    from Articulate blog. However, I am not sure how to do this because I do not know what using directives to include, etc.?

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 23, 2017 @ 10:26
    Owain Williams
    0

    Hi Bobi, This is how I've done it - my homepage pulls in posts from two different authors but you may be able to work with my code and get yours working.

    My partial looks like this:

    @inherits UmbracoTemplatePage
    
        @{
            var blogNodes = @Model.Content.AncestorOrSelf(1).Descendants("ArticulateRichText").OrderBy("publishedDate desc").Take(6);
        }
    
    
        @foreach (var blogNode in blogNodes)
    {
    
    
        var blogTitle = @blogNode.Name;
    
        var tempExcerpt =  @blogNode.GetPropertyValue("excerpt").ToString();
        const int MaxLength = 90;
    
    
    
    
    
        <div class="col-md-4 col-xs-12 blogBox">
    
    <div class="image">
      @if (@blogNode.GetCropUrl("croppedImage", "thumbnail") != "")
                        {
    
                          @*  var imageId = blogNode.GetPropertyValue("featuredImage"); *@
                           @* var altText = blogNode.GetPropertyValue("alternativeImageText"); *@
    
                            <a href="@blogNode.Url">
                                <img src="@blogNode.GetCropUrl("croppedImage", "thumbnail")" alt="@blogNode.GetPropertyValue("altText")"  class="img-responsive" />
                                    </a>
                        }
                        else
                        { 
                            <a href="@blogNode.Url">     <img src="@Umbraco.Media(1925).Url" alt="A blog post" class="img-responsive" /></a>
                        } 
    
    
    
      </div>
          <div class="copy" style="padding: 0 5px 5px 5px;">
              <h4 class="media-heading"><a href="@blogNode.Url">@blogTitle</a></h4>
              <h5>@(blogNode.GetPropertyValue<DateTime>("publishedDate").ToString("dd/MM/yyyy"))</h5>
    
    
          @if (tempExcerpt.Length > MaxLength)
        {
    
            tempExcerpt = tempExcerpt.Substring(0, MaxLength)+"...";
        }
        <p>@tempExcerpt</p>
    
    <div class="author" style="border-top: 1px solid #ccc; padding: 0 5px 5px 5px;">
    <h6>
     By @blogNode.GetPropertyValue("author")
     </h6> 
     </div>
    
    
    
         <div class='clear'></div>
    </div>
        </div>
    
    
    
    
    }
    

    Then I on my homepage template I just use:

        @Html.Partial("umbLatestBlogWidget")
    

    umbLatestBlogWidget is the name of my partial.

    Hope that helps.

    You can see how this looks on my website www.runningbeside.me

  • Bobi 352 posts 956 karma points
    Feb 23, 2017 @ 10:50
    Bobi
    0

    Thanks so much Owain,

    I will try it out and let you know. What do your using directives and @inherits look like on your homepage?

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 23, 2017 @ 10:56
    Owain Williams
    0

    Homepage:

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    }
    

    That's it. :)

  • Bobi 352 posts 956 karma points
    Feb 23, 2017 @ 11:08
    Bobi
    0

    Thanks so much. I will try it out and let you know. I'm assuming your partial is stored in App_Plugins/Articulate/Themes/"Theme Name"/Views/Partials.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 23, 2017 @ 11:11
    Owain Williams
    0

    Hi Bobi, No my partial is kept within root/views/partials not within Articulate itself.

    I set it up via the backoffice just in the usual partials folder 'settings/patrialviews/'

  • Bobi 352 posts 956 karma points
    Feb 23, 2017 @ 15:52
    Bobi
    0

    Ok, so I tried the same code as your partial in my partial, and then tried to call it. Everything is running, except whatever is supposed to show up from your partial isn't ... ?

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 23, 2017 @ 15:57
    Owain Williams
    0

    Hi, Here is how I have articulate setup in the backoffice:

    enter image description here

    So I have a doctype called Blog Pages which I then nest Articulate within. You may need to change

    var blogNodes = @Model.Content.AncestorOrSelf(1).Descendants("ArticulateRichText").
    

    Change the (1) to another number, if you have your Articulate blog sitting under the root, maybe change the 1 to a 0.

  • Bobi 352 posts 956 karma points
    Feb 23, 2017 @ 16:29
    Bobi
    0

    Hi, thanks again for following-up. You've been extremely helpful in what I can only describe as one of the most frustrating of things...

    My content appears as so:

    enter image description here

    index is my supposed to be my homepage...

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 23, 2017 @ 16:47
    Owain Williams
    1

    Ok, this make a bit more sense then why you are having issues.

    What I would do is allow Articulate to be a child of the index document type.

    Click on Index then on the Properties tab on the right hand side, click the Document Type. Once that loads, click on Permissions up in the top right hand corner. Then click the Add Child button and add Articulate. Save.

    Once you have done this, right click on the Articles link under content which you have shown in your screenshot. Click Move in the sub menu and select your Index page. This should move your Articles under Index.

    That should hopefully help get everything showing like I have it :)

  • Bobi 352 posts 956 karma points
    Feb 24, 2017 @ 01:04
    Bobi
    0

    Thank you so much, your code works! This is brilliant! Thank you so much! I have only a few strands of hair left after pulling it all out over frustration over this. Thanks Owain!

    PS. I checked out your blog; looks awesome.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 24, 2017 @ 09:09
    Owain Williams
    1

    Glad I could help. I've had so much help on here that it's nice that I can actually help someone else for a change :)

    O.

  • Bobi 352 posts 956 karma points
    Feb 24, 2017 @ 17:45
    Bobi
    0

    You wouldn't happen to know a way to implement a full site search would you? I have a search for the blog, but am thinking of implementing a full site search.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 24, 2017 @ 18:14
    Owain Williams
    0

    Hi, Yip, I followed these tutorials :

    https://www.youtube.com/watch?v=b5wbazkehH8

    or

    advanced search: https://www.youtube.com/watch?v=b5wbazkehH8

    I've also got a thread on here talking about implementing a fuzzy search but I've not had a chance to try out the code.

  • Bobi 352 posts 956 karma points
    Feb 26, 2017 @ 00:07
    Bobi
    0

    Thanks Owain. I used the simple one too...It works great. I'm trying to figure out how to display an excerpt (some text in front and behind of the search term query, so that it can be called and displayed underneath the @result.Name; and then ideally, to highlight the search text by bolding it. Any knowledge as to how to go about that?

    I tried the advanced tutorial too, but unfortunately, my site does not use field properties, so it's not too effective, since the advanced tute relies on those fields.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Feb 27, 2017 @ 09:36
    Owain Williams
    0

    I've not had a chance to work with the search stuff much yet. Been busy on a separate project but I'll give you a shout if I do get back on it soon :)

  • Bobi 352 posts 956 karma points
    Mar 02, 2017 @ 22:04
    Bobi
    0

    Thanks again Owain. Good-luck with the new project.

    Would you know how to manipulate the code you provided (for the displaying of recent posts on homepage) to do the same thing, but instead only show posts in certain categories or in certain tags?

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Mar 03, 2017 @ 09:35
    Owain Williams
    0

    Hi Bobi, I had a quick play with this last night.

    What I've managed to do is pull from a tag by changing the initial lookup:

    var blogNodes = @Model.Content.AncestorOrSelf(1).Descendants("ArticulateRichText").OrderBy("publishedDate desc").Take(6).Where("tags.Contains(\"running\")");
    

    You will see I've added a Where clause. There is one error that I've found though. If there is a list of tags e.g. running, training, somethingelse - it will display the running blog perfectly, however, if running is not first, e.g. training, running, somethingelse, the blog isn't displayed.

    To fix this I think you'd need to pull the tags and maybe iterate through a loop check each tag. I'll see if I can get time to test this and use it as my next blog post on my new umbraco site :) www.wildsitecreations.co.uk

    Let me know if you manage to get it to work.

  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Mar 03, 2017 @ 09:59
    Owain Williams
    0

    I was hoping something like this would work but I get an error:

    var blogNodes = @Model.Content.AncestorOrSelf(1).Descendants("ArticulateRichText").Where("tags.Split(',').Contains(\"running\")").OrderBy("publishedDate desc").Take(6);
    

    Maybe someone else will be able to say where I'm going wrong.

    Error: Umbraco.Core.Dynamics.ParseException: No applicable method 'Split' exists in type 'String'

  • Bobi 352 posts 956 karma points
    Mar 03, 2017 @ 20:48
    Bobi
    0

    That's brilliant. I'm noticing the same issue where only one blog post is being pulled with the specified tag or category, even if "running" is first in another post.

  • Bobi 352 posts 956 karma points
    Mar 03, 2017 @ 23:54
    Bobi
    0

    Owain,

    After some help from Kris Janssen (another helpful member) and a bit of tinkering, I ended up with the following, which works to filter by category. I hope this helps you make your method above work, since I like the simplistic nature of it. If you figure it out, feel free to let me know :)

    @using Articulate
    @using Articulate.Models
    @using System.Linq
    
    @inherits UmbracoTemplatePage
    
    @{
        // EDIT: This node id is unlikely to ever change so hardcoding it should be OK.
        var newsroot = Umbraco.TypedContent(1066);
        var blogArchive = newsroot.Children.First();
        var categories = Umbraco.TagQuery
                            .GetAllContentTags("ArticulateCategories")
                            .Select(x => x.Text)
                            .OrderBy(x => x);
        var categoriesUrl = newsroot.GetPropertyValue<string>("categoriesUrlName");
    
    // EDIT: Get the Children here once instead of repeatedly
        var entries = blogArchive.Children();
        var postDate = newsroot.CreateDate.ToString("MMMM d, yyyy");
        string c = "Test";
    }
    
    <!--=== News Block ===-->
    <div class="bg-grey">
    <div class="container content-sm">
        <div class="text-center margin-bottom-50">
            <h2 class="title-v2 title-center">RECENT NEWS</h2>
        </div>
        <div class="row news-v1">
    
            @if (c == "Test")
            {
                var filteredEntries = entries
                       // EDIT: Splitting the property value is not needed ...
                       .Where(x => x.GetPropertyValue<string>("categories") != null && x.GetPropertyValue<string>("categories").InvariantContains(c))
                       //EDIT: Only order after selecting for the categories, less to order that way
                       .OrderByDescending(x => x.UpdateDate)
                       .Take(3)
                       .ToList();
    
    
    
    
                foreach (var p in filteredEntries)
                {
                   var blogTitle = @p.Name;
                    <div class="col-md-4 md-margin-bottom-40">
                        <div class="news-v1-in bg-color-white">
                            <a href="@(newsroot.Url + "/" + categoriesUrl + "/" + c)"><img class="img-responsive" src="@p.GetCropUrl("postImage", "indexBlogPost")" title="@p" alt="@p" /></a>
    
                            <h3 class="font-normal"><a href="@(newsroot.Url + "/" + categoriesUrl + "/" + c)">@blogTitle</a></h3>
    
                            <p>@(Umbraco.Truncate(p.GetPropertyValue<string>("excerpt"), 75))</p>
                            <ul class="list-inline news-v1-info no-margin-bottom">
                                <li><i class="fa fa-clock-o"></i> @postDate</li>
                            </ul>
                        </div>
                    </div>
                }
            }
            @*else if (c != "Test")
            {
                <div class="col-md-4 md-margin-bottom-40">
                    <div class="news-v1-in bg-color-white">
    
                        <h3>No News</h3>
    
                    </div>
                </div>
            }
                *@
        </div>
    </div>
    </div>
    <!--=== End News Block ===-->
    

    It works, which is nice. I am just trying to figure out how to now display some generic html code (i.e. No recent news) if there are no articles in the specified category.

    Edit

    I got things working. There may be inefficiencies, but nevertheless here it is:

    @using Articulate
    @using Articulate.Models
    @using System.Linq
    @inherits UmbracoTemplatePage
    @{
    // EDIT: This node id is unlikely to ever change so hardcoding it should be OK.
    var newsroot = Umbraco.TypedContent(1066);
    var blogArchive = newsroot.Children.First();
    var categories = Umbraco.TagQuery
                            .GetAllContentTags("ArticulateCategories")
                            .Select(x => x.Text)
                            .OrderBy(x => x);
    var categoriesUrl = newsroot.GetPropertyValue<string>("categoriesUrlName");
    
    // EDIT: Get the Children here once instead of repeatedly
    var entries = blogArchive.Children();
    
    var filteredEntries = entries
                       // EDIT: Splitting the property value is not needed ...
                       .Where(x => x.GetPropertyValue<string>("categories") != null && x.GetPropertyValue<string>("categories").InvariantContains("Test"))
                       //EDIT: Only order after selecting for the categories, less to order that way
                       .OrderByDescending(x => x.CreateDate)
                       .Take(3)
                       .ToList();
    }
    
    <!--=== News Block ===-->
    <div class="bg-grey">
    <div class="container content-sm">
        <div class="text-center margin-bottom-50">
            <h2 class="title-v2 title-center">RECENT NEWS</h2>
        </div>
        <div class="row news-v1">
    
    
            @if (filteredEntries == null || filteredEntries.Count() == 0)
            {
                <div class="md-margin-bottom-40 text-center">
                        <h3 class="font-normal">No Recent News</h3>
                </div>
            }
            else
            {
                foreach (var blogNode in filteredEntries)
                {
                    var blogTitle = @blogNode.Name;
                    var tempExcerpt = @blogNode.GetPropertyValue("excerpt").ToString();
                    var postDate = blogNode.CreateDate.ToString("MMMM d, yyyy");
                    const int MaxLength = 133;
    
                    <div class="col-md-4 md-margin-bottom-40">
                        <div class="news-v1-in bg-color-white">
                            <a href="@blogNode.Url"><img class="img-responsive" src="@blogNode.GetCropUrl("postImage", "indexBlogPost")" title="@blogTitle" alt="@blogTitle" /></a>
                            <h3 class="font-normal"><a href="@blogNode.Url">@blogTitle</a></h3>
                            @if (tempExcerpt.Length > MaxLength)
                            {
                                tempExcerpt = tempExcerpt.Substring(0, MaxLength) + "...";
                            }
                            <p>@tempExcerpt</p>
                            <ul class="list-inline news-v1-info no-margin-bottom">
                                <li><i class="fa fa-clock-o"></i> @postDate</li>
                            </ul>
                        </div>
                    </div>
                }
            }
        </div>
    </div>
    </div>
    <!--=== End News Block ===-->
    
  • Owain Williams 482 posts 1414 karma points MVP 7x c-trib
    Mar 10, 2017 @ 12:07
    Owain Williams
    1

    Great work! :) Glad you got it working.

    O.

  • Bobi 352 posts 956 karma points
    Apr 26, 2017 @ 21:39
    Bobi
    0

    Hi all,

    I am playing around with the idea of changing my node structure due to Articulate's longer than desired url when Articulate is added as a child to your homepage.

    Currently my site structure is (first scenario):

    -Homepage
      -->Page 1
      -->Articulate
          --Archive
          --Authors
    

    I'm trying to avoid the dreaded url example.com/articulate/archive/first-post for obvious SEO reasons. As well there are duplicate pages when it comes to example.com/articulate and example.com/articulate/archive.

    As such, I'm looking to structure the site like so (second scenario):

    -Homepage
      -->Page 1
    -Articulate
      -->Archive
      -->Authors
    

    As you can see the Articulate node is at the root, along with Homepage.

    However, changing this mucks things up when it comes to pulling in recent posts to Homepage. Currently, I'm using the following to pull in recent blog posts:

    var blogNodes = @Model.Content.AncestorOrSelf(1).Descendants("ArticulateRichText").OrderBy("publishedDate desc").Take(4); 
    

    This works great for the first scenario, but not in the second updated node structure.

    How can a modify the above code to work in the second scenario?

Please Sign in or register to post replies

Write your reply to:

Draft