Copied to clipboard

Flag this post as spam?

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


  • Andreas Pfanner 196 posts 314 karma points
    May 11, 2016 @ 15:52
    Andreas Pfanner
    0

    Embed posts by category or tag to another page

    Hi,

    I want to render blog posts of a specific category or tag inside a standard text page of my web site, so it should not be accessed through the default Articulate /tags/ or /categories URLs but directly embedded.

    My idea is to use @Html.Partial("List.cshtml", ...), but I am stucked with getting the Model for passing to the partial.

    Any hints?

    Best Regards Andreas

  • Matt Brown 62 posts 174 karma points
    May 17, 2016 @ 17:35
    Matt Brown
    1

    I need something very much like this where I can access the last three items (or whatever arbitrary number) on the home page of the site. So on the blog there are these four entries:

    1. My dog caught a ball. May 17, 2016
    2. My cat caught a mouse. May 15, 2016
    3. The rat was bothering the cat. May 14, 2016
    4. The trees were green. May 12, 2015

    On my home page I want to have the first three on my home page.

  • Matt Brown 62 posts 174 karma points
    May 18, 2016 @ 17:03
    Matt Brown
    0

    OK. More progress.

    I found this code:

    @{
        var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
        var blogArchive = blogRoot.Children.First();
    
        foreach(var blogposts in blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")){
            <p>@blogposts.Name</p>
        }
    }
    

    And I added it to my home page and I now get the Name of each blog post in the archive listed on my home page. However I want the content of the post not the name.

    Question:

    1. What do I need to replace @blogposts.Name with to get the body of the blog post?

    2. How can I get a list of the keys in blogspots so I can pick and choose what things in the post to display?

  • Matt Brown 62 posts 174 karma points
    May 24, 2016 @ 21:57
    Matt Brown
    1

    OK Yet more progress...

    I finally found out how to get the key in the object by looking in the cmsContentXml table in the Umbraco.sdf.

    In the XML column you can find the content and then use the tags there to get the names to put in blogposts.Name

    To get the article content from the richText editor, you use blogposts.richText and they are then available on any page you need them with the following code.

    @{
        var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
        var blogArchive = blogRoot.Children.First();
    
        foreach(var blogposts in blogArchive.Children.Where("Visible").Where("categories == @0","TestCategory")){
            <p>@blogposts.richText</p>
        }
    }
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    May 25, 2016 @ 17:28
    Dennis Aaen
    0

    Hi Matt,

    Each of your fields on your document type has an alias, and it´s by this alias that you can get data from a field in Umbraco.

    So in your case the field for the article content has a alias of richText.

    Try to see this video, that describes the concepts https://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/document-types/tabs-and-properties/

    Hope this helps,

    /Dennis

  • Andreas Pfanner 196 posts 314 karma points
    May 25, 2016 @ 05:30
    Andreas Pfanner
    0

    Hi Matt,

    thanks for the input! I will try that.

    Best Regards Andreas

  • Matt Brown 62 posts 174 karma points
    May 25, 2016 @ 16:38
    Matt Brown
    0

    Please ping me if it doesn't work. I am more than excited to share what I found if it helps you.

  • Mark 11 posts 82 karma points
    May 26, 2016 @ 17:15
    Mark
    0

    Hi Matt,

    Did you figure out how to get all bog posts by Tag?

    Cheers Mark

  • Matt Brown 62 posts 174 karma points
    May 26, 2016 @ 17:34
    Matt Brown
    0

    Yes. You change the foreach Where("categories== @0","testCategory") to .Where("tags == @0","wolf")

    Your code would look like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @{
            Layout = "Master.cshtml";
        }
        @{
            var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
            var blogArchive = blogRoot.Children.First();
    
            foreach(var blogposts in blogArchive.Children.Where("Visible").Where("tags == @0","wolf")){
                <p>@blogposts.richText</p>
            }
        }
    
        @CurrentPage.GetGridHtml("content", "fanoe")
    
  • Mark 11 posts 82 karma points
    May 26, 2016 @ 17:56
    Mark
    0

    Hi Matt,

    Thanks for your reply...yeah I tried that but nothing is returned. If I bring back all posts and just display the tags property (@blogposts.tags) in the page I can see the tag I'm trying to select on is in the list though!

  • Mark 11 posts 82 karma points
    May 26, 2016 @ 18:02
    Mark
    0

    aah...dynamic linq...do I need to include a namespace?

  • Mark 11 posts 82 karma points
    May 26, 2016 @ 18:08
    Mark
    1

    Got it...thanks for your help...I have multiple tags per post! So this works for me...

    var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
    var blogArchive = blogRoot.Children.First();
    
    foreach (var blogposts in blogArchive.Children.Where("Visible").Where("tags.Contains(@0)", "wolf"))
     {
         <p>@blogposts.tags</p>
     }
    
  • Matt Brown 62 posts 174 karma points
    May 26, 2016 @ 18:09
    Matt Brown
    0

    COOL! Thank you. I am will update my code as well. This has been a good interaction. Thank you!

  • Matt Brown 62 posts 174 karma points
    Aug 24, 2016 @ 19:38
    Matt Brown
    0

    OK More questions. How do I limit the return to only two or three items?

    Right now the blog items are coming into my page with the code above from Mark in the order of oldest to newest. How can I change that to newest to oldest.

    My goal is to display only the most recent two blog entries in my page. I am successfully getting them all but in reverse order.

    Currently:
    My first blog entry - Monday, may 1
    My second blog entry - Tuesday may 2
    My third blog entry - Wednesday may 3
    My fourth blog entry - Thursday may 4
    
    I want (limit 2):
    My fourth blog entry - Thursday may 4
    My third blog entry - Wednesday may 3
    
    I want (limit 3):
    My fourth blog entry - Thursday may 4
    My third blog entry - Wednesday may 3
    My second blog entry - Tuesday may 2
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Aug 24, 2016 @ 20:02
    Dennis Aaen
    0

    Hi Matt,

    To sort it to news to old you could use the method .OrderBy("desc") and for limit the numbers of items that you get out, you could use the method Take(int)

    So the code should look something like this.

    var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
    var blogArchive = blogRoot.Children.First();
    
    foreach (var blogposts in blogArchive.Children.Where("Visible").Where("tags.Contains(@0)", "wolf").OrderBy("publishedDate desc").Take(3))
     {
         <p>@blogposts.tags</p>
     }
    

    Hope this helps,

    /Dennis

  • Matt Brown 62 posts 174 karma points
    Aug 24, 2016 @ 20:22
    Matt Brown
    0

    OK that is a big help and Take(x) works great. However the OrderBy("desc") gives an error of System.InvalidOperationException:

    The solution is to add the field that you want to sort on in the OrderBy() so that would be OrderBy("publishedDate desc")

    So working code for me looks like this:

    @{
                var blogRoot = Umbraco.ContentAtRoot().DescendantsOrSelf("Articulate").FirstOrDefault();
                var blogArchive = blogRoot.Children.First();
    
                foreach (var blogposts in blogArchive.Children.Where("Visible").Where("tags.Contains(@0)", "wolf").OrderBy("publishedDate desc").Take(2))
                 {
                     <h4 class="media-heading">@blogposts.umbracoUrlName - @blogposts.publishedDate</h4>
                     <p> @blogposts.excerpt </p>
                 }
        }
    

    Thanks again for the help on this.

Please Sign in or register to post replies

Write your reply to:

Draft