Copied to clipboard

Flag this post as spam?

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


  • Kristian Frost 18 posts 102 karma points
    Dec 10, 2009 @ 14:19
    Kristian Frost
    0

    Sorting output by date

    Hi all!

    Im making a "News"-page at my website and would like to extract the latest news with xslt, and order them by date. How can I do that?
    I've tried to do like this:

    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node [string(data [@alias='umbracoNaviHide']) != '1'] [position() &lt; last() - number($count)]">

    Where $source is my newsarchive and $count is the number of latest news to pull out.

    /Kristian

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Dec 10, 2009 @ 14:24
    Sebastiaan Janssen
    0

    The sorting can simply be done by doing an xsl:sort right below your for-each (it has to be the first thing after the for-each statement.

    <xsl:sort select="your_date_property" order="descending"/>
  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Dec 10, 2009 @ 14:26
    Sebastiaan Janssen
    3

    A full example, the XSLT for a list of my latest blog posts looks like this:

    <xsl:param name="currentPage"/>
    <xsl:variable name="numberOfPosts" select="3"/>
    
    <xsl:template match="/">
    
    <xsl:for-each select="$currentPage/ancestor-or-self::node //node[@nodeTypeAlias = 'BlogPost']">
    <xsl:sort select="@createDate" order="descending" />
        <xsl:if test="position() = 1">
         <h2>Latest blog posts</h2>
        </xsl:if>
        <xsl:if test="position() &lt;= $numberOfPosts">
            <p><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></p>
        </xsl:if>
    </xsl:for-each>
  • Kristian Frost 18 posts 102 karma points
    Dec 10, 2009 @ 14:30
    Kristian Frost
    0

    Thanks Sebastiaan, that solved it :)

  • siva kumar 120 posts 209 karma points
    Oct 09, 2012 @ 05:57
    siva kumar
    0

    hi all,

    how to get top 10 news by using date and year (without considering date) in razor 

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 09, 2012 @ 06:23
    Fuji Kusaka
    0

    Hi Siva,

    You could achieve this by making use of OrderBy and Take. Where you will only display 10 news items and make a sorting out of it.

    for e.g

    foreach(dynamic news in newsFolder.Children.OrderBy("createDate descending").Take(10)){
    @news.Name // here am making use of createDate but you can also add a date picker property
    }
  • 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