Copied to clipboard

Flag this post as spam?

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


  • James 251 posts 1169 karma points
    Oct 14, 2014 @ 11:29
    James
    0

    Displaying Blog Posts

    Hello All,

     

    I am trying to display blog posts with a certain property that I have assigned them (using a custom drop down type i created).

     

    I have used this code in a previous project.

     

    Is there anyway to modify the following to search for blog posts with a property of "funny" or "romantic" for example.

     

    @foreach(var page in CurrentPage.DescendantsOrSelf("BlogPost").OrderBy("CreateDate desc"))

    {

    //Do Stuff

    }

    Kind regards.

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Oct 14, 2014 @ 11:38
    Dennis Aaen
    0

    Hi James,

    Can“t you do something simlar what you did on on the category. http://our.umbraco.org/forum/umbraco-7/using-umbraco-7/57313-Searching-for-a-category?p=0#comment195615

    @{
    //moving to the page with the child items
    //if you are on the blog page with children you can get the children by using Model.content.Children()
    var home = Model.Content.AncestorsOrSelf("Home").First();
    var blogs= home.Descendants("Blog").First();
    }

    @foreach (var blog in blogs.Descendants("BlogPost").Where(x => x.GetPropertyValue("type").Equals("funny")))
    {
        // do things with blogpost
    }

    Hope this helps,

    /Dennis

  • James 251 posts 1169 karma points
    Oct 14, 2014 @ 11:44
    James
    0

    I'm actually having trouble with that code.

     

    The page loads once I have put in the custom property type "Blog Category" and "Funny". 

     

    Inside the code block I put what i want to be displayed (the blog post structure). But nothing loads? I have tried to modify the code but to no avail.

     

    The structure of my site is:

     

    Home > Blog > Blog Posts

     

    I am trying to render the code on the "Home" Page template.

     

    So far:

     

    @{

    //moving to the page with the child items

    //if you are on the blog page with children you can get the children by using Model.content.Children()

    var home = Model.Content.AncestorsOrSelf("Home").First();

    var blogs= home.Descendants("BlogPost").First();

    }

     

    @foreach (var blog in blogs.Descendants("BlogPost").Where(x => x.GetPropertyValue("Blog Category").Equals("Funny")))

    {

       <span class="date">

    29 <small>JUN 2014</small>

    </span>

    }

     

    Page Loads fine but nothing displays so its not pulling anything through?

     

    Kind regards.

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    Oct 14, 2014 @ 11:51
    Dennis Aaen
    100

    Hi James,

    If your blogpost is structure under blog I think that you could do something like this:

    @{
    //moving to the page with the child items
    //if you are on the blog page with children you can get the children by using Model.content.Children()
    var home =Model.Content.AncestorsOrSelf("Home").First();
    var blogs= home.Descendants("Blog").First();
    }

    @foreach(var blog in blogs.Children.Where(x => x.GetPropertyValue("blogCategory").Equals("funny")))
    {
       
    // do things with blogpost
    }

    I assume that blogCategory is the alias of the field where the user should choose category for each post.

    Hope this helps,

    /Dennis

  • James 251 posts 1169 karma points
    Oct 14, 2014 @ 11:57
    James
    0

    Thanks Dennis,

     

    Again something very simple. One of these days I'll learn to solve this sort of stuff on my own. :)

     

    Your code was correct. My assumption that my Alias was blog category was incorrect however.

     

    My alias was in fact: blogPostCategory.

     

    Everything now works. Including the code in the previous topic.

     

    Kind regards,

     

    J

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies